是否可以添加多个' on'在Linq的1份加入声明中

时间:2014-08-06 03:53:23

标签: c# mysql sql linq asp.net-mvc-3

我不知道在LINQ中可以在一个连接语句中嵌套或者有两个或更多on c.blah equals b.blah,因为我必须加入表而不影响数据的数量

因为如果我将其添加到where,则会减少数据量并将其作为过滤器

我目前尝试的是添加&&条款,但它无效

var Pos =  (from a in db.Position
            join b in db.Position_Location
             on a.ID equals b.PositionId
            join c in db.Customer
            on a.CustomerID equals c.ID
            join d in db.Customer_Location
            on b.LocationId equals d.ID 
            join f in db.Worker
             on userIdNew equals f.userId
            join e in db.Worker_Customer_Apply_Shift <----Planning to add new validation here
                     on a.ID equals e.Client_Customer_PositionID into trial
                 from newtrial in trial.DefaultIfEmpty()
                 where 
                       b.LogicalDelete == false
                       && a.LogicalDelete == false
                       && c.LogicalDelete == false
                       && d.LogicalDelete == false

                 select new
                 {
                     a.ID,
                     Client_CustomerID = c.ID,
                     LogicalDelete =(newtrial == null ? true : newtrial.LogicalDelete),


                 }).Distinct().ToList();

提前致谢:)

1 个答案:

答案 0 :(得分:2)

您可以使用匿名类型以及要在连接条件中使用的字段:

on new { a.One, a.Two } equals new { b.One, b.Two }

如果两个表中的列不具有相同的名称,则需要提供匿名类型属性的名称:

on new { Col1 = a.One, Col2 = a.Two } equals new { Col1 = b.Three, Col2 = b.Four }