使用左右连接和嵌套查询对linq进行SQL查询

时间:2013-12-02 16:47:50

标签: c# sql linq

我的SQL查询是

SELECT DISTINCT 
  tblProjects.RevID, tblProjects.CEQRNum, tblProjects.ProjectName, GEOCODE.BBL,     tblProjects.BoroID, GEOCODE.BLOCK, GEOCODE.LOT, 
                     tblMilestoneType.MilestoneName, tblMilestone.MilestoneDate
FROM            tblMilestoneType INNER JOIN
                     tblMilestone ON tblMilestoneType.MilestoneID = tblMilestone.MilestoneTypeID RIGHT OUTER JOIN
                     tblProjects ON tblMilestone.RevID = tblProjects.RevID LEFT OUTER JOIN
                     GEOCODE ON tblProjects.RevID = GEOCODE.RevID
WHERE        (
            tblMilestone.MilestoneDate IN (SELECT MAX(tblMilestone.MilestoneDate) AS MilestoneDate
                                          FROM tblMilestone INNER JOIN tblMilestoneType ON tblMilestone.MilestoneTypeID = tblMilestoneType.MilestoneID
                                          GROUP BY tblMilestone.RevID)
         )
ORDER BY tblProjects.RevID

我正在尝试将其转换为linq。

所以我开始

projectList = db.tblMilestoneTypes
                                .Join(db.tblMilestones,
                                    mileType => mileType.MilestoneID,
                                    mile => mile.MilestoneTypeID,
                                    (mileType, mile) => new
                                    {
                                        tblMilestoneType = mileType,
                                        tblMilestone = mile
                                    })
                                .Join(db.tblProjects,                                        
                                    project => project.        // does not work)

也试过

projectList = from MilestoneTypeTable in db.tblMilestoneTypes 
                          join MilestoneTable in db.tblMilestones  on MilestoneTypeTable.MilestoneID equals MilestoneTable.MilestoneTypeID
                          rig   // no right or left join.

我该怎么做 NEHA

2 个答案:

答案 0 :(得分:0)

您可以尝试

projectList = from MilestoneTypeTable in db.tblMilestoneTypes
                      join MilestoneTable in db.tblMilestones on MilestoneTypeTable.MilestoneID equals MilestoneTable.MilestoneTypeID into MileStones 
from mileStone in MileStones 
join from projects in db.tblProjects
on mileStone.RevID  equals projects.RevID
select new { MileStoneType = MilestoneTypeTable, MileStones = mileStone , Project = projects };

答案 1 :(得分:0)

经过很多研发后这就是我发现的

  1. 具有LEFT OUTER JOIN和RIGHT OUTER JOIN的T-SQL可以转换为只有LEFT OUTER JOIN的一个查询

  2. 将子查询作为加入

  3. 的一部分

    这是查询。

        var query = from ProjectTable in db.tblProjects
                            from GeoCodeTable in db.GEOCODEs.Where(geo => geo.RevID == ProjectTable.RevID).DefaultIfEmpty()
                            from MilestoneTable in db.tblMilestones.Where(mile => mile.RevID == GeoCodeTable.RevID && mile.MilestoneDate == db.tblMilestones.OrderBy(x => x.RevID).Select(x => x.MilestoneDate).Max()).DefaultIfEmpty()
                            from MilestomeTypeTable in db.tblMilestoneTypes.Where(mt => mt.MilestoneID == MilestoneTable.MilestoneTypeID).DefaultIfEmpty()
                select new
                {
                    Milestone = MilestoneTable,
                    Project = ProjectTable,
                    GeoCode = GeoCodeTable,
                    MilestomeType = MilestomeTypeTable 
                };