简体   繁体   中英

How to use AND operator in LINQ Join

I have following SQL Query

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        

As I have try convert the same in to LINQ but I can not due to AND OPERATOR use in Inner Join.

I am not aware how to use And Operator in LINQ , JOIN

As folloing the linq code which i try.

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())

As i have try with && but it did not work....

it is done as 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}

Move the AND condition to your WHERE clause. Writing

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

is exactly the same as writing

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM