4

我有以下 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 在 Inner Join 中的使用,我不能。

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

正如我尝试的 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())

正如我尝试过 && 但它没有用....

4

3 回答 3

5

它是这样完成的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}
于 2012-08-20T11:11:05.610 回答
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*
于 2012-08-20T10:58:49.903 回答
-2

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

于 2012-08-20T11:00:06.080 回答