簡體   English   中英

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

[英]How to use AND operator in LINQ Join

我有以下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運算符而無法使用。

我不知道如何在LINQ中使用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())

因為我嘗試過&&但它沒有用....

就像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}

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*

我認為您需要使用where運算符代替&&。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM