如何在LINQ Join中使用AND运算符

时间:2012-08-20 10:54:45

标签: c# linq linq-to-sql linq-to-entities linq-to-objects

我有以下SQL查询

SELECT * FROM KDMS_dynamic vd

INNER JOIN KDMS_definition tblKDMS  ON tblKDMS.SystemID=vd.SystemID    
LEFT OUTER JOIN KDMS_typeid tblKDMSType ON tblKDMS.TypeId=tblKDMSType.TypeId            
INNER JOIN  KDMS_configuration tblKDMSConfig ON tblKDMS.SystemID=tblKDMSConfig.SystemID

    AND tblKDMSConfig.ConfigurationDate = (SELECT MAX(ConfigurationDate)
                                        FROM KDMS_configuration vc
                                        WHERE vc.SystemID=tblKDMSConfig.SystemID)
    AND vd.LastUpdated=(SELECT MAX(LastUpdated) FROM KDMS_dynamic vd 
                        WHERE vd.SystemID=tblKDMS.SystemID)  

        WHERE  

         DeletionDate IS NULL             
      AND LongDescription IS NOT NULL       
      AND tblKDMS.TypeId <> 1        

我尝试将相同的内容转换为LINQ,但由于AND OPERATOR在内部连接中使用,我不能这样做。

我不知道如何在LINQ,JOIN中使用And运算符

接下来我尝试的linq代码。

IQueryable<getKDMS> query = (from VD in this._db.GetTable<KDMS_dynamic>()
                                        join TblKDMS in this._db.GetTable<KDMS_definition>() on VD.SystemID equals TblKDMS.SystemID
                                        join TblKDMSType in this._db.GetTable<KDMS_typeid>().DefaultIfEmpty() on TblKDMS.TypeID equals TblKDMSType.TypeID 
                                        join TblKDMSConfig in this._db.GetTable<KDMS_configuration>() on TblKDMS.SystemID equals TblKDMSConfig.SystemID

                                        &&  TblKDMSConfig.ConfigurationDate == (from TblKDMS_conf in this._db.GetTable<KDMS_configuration>()
                                                                                      where TblKDMS_conf.SystemID == TblKDMSConfig.SystemID
                                                                                      select TblKDMS_conf.ConfigurationDate).Max())

我试过&amp;&amp;但它没有用......

3 个答案:

答案 0 :(得分:4)

on new{x.field1,x.field2} equals new{y.field1,y.field2}

完成
var somedata = (from TblKDMS_conf in this._db.GetTable<KDMS_configuration>()
              where TblKDMS_conf.SystemID == TblKDMSConfig.SystemID select TblKDMS_conf.ConfigurationDate).Max();

Queryable<getKDMS> query = (from VD in this._db.GetTable<KDMS_dynamic>()
                                    join TblKDMS in this._db.GetTable<KDMS_definition>() on VD.SystemID equals TblKDMS.SystemID
                                    join TblKDMSType in this._db.GetTable<KDMS_typeid>().DefaultIfEmpty() on TblKDMS.TypeID equals TblKDMSType.TypeID 
                                    join TblKDMSConfig in this._db.GetTable<KDMS_configuration>() on new {TblKDMS.SystemID,TblKDMSConfig.ConfigurationDate} equals new{TblKDMSConfig.SystemID,somedata}

答案 1 :(得分:2)

AND条件移至WHERE子句。写作

SELECT * FROM Table1
INNER JOIN Table2 ON *first condition* AND *second condition*
WHERE *third condition*

与写作完全相同

SELECT * FROM Table1
INNER JOIN Table2 ON *first condition*
WHERE *second condition* AND *third condition*

答案 2 :(得分:-2)

我认为您需要使用where运算符代替&amp;&amp;。