简体   繁体   English

在LINQ JOIN中添加第二个条件,

[英]Adding second condition in LINQ JOIN,

I have looked around and found a few posts on adding a second condition to a JOIN clause but they are always instances of having one column link to another but in my instance I need to just have a column equal a certain value. 我环顾四周,发现了一些关于向JOIN子句添加第二个条件的帖子,但它们总是有一个列链接到另一个的实例,但在我的实例中我需要一个列等于某个值。 The RAW SQL I am trying to imitate is: 我试图模仿的RAW SQL是:

LEFT OUTER JOIN dbo.TimeCardHeader AS tch2 on tch1.EmployeeID = tch2.EmployeeID && tch2.WeekEndingDate = @PayPeriod

As you can see my second join condition is to match a column value from the table to a variable. 正如您所看到的,我的第二个连接条件是将表中的列值与变量匹配。 I have tried the following LINQ queires but they all fail. 我尝试了以下LINQ队列,但它们都失败了。

join leftth2 in db.TimeCardHeaders on th1.EmployeeID equals leftth2.EmployeeID AND leftth2.WeekEndingDate == PayPeriod into leftjointh2

join leftth2 in db.TimeCardHeaders on th1.EmployeeID equals leftth2.EmployeeID && leftth2.WeekEndingDate == PayPeriod into leftjointh2

join leftth2 in db.TimeCardHeaders on new
{
    employeeID = th1.EmployeeID,
    weekEndingDate = leftth2.WeekEndingDate
} equals new
{
    employeeID = leftth2.EmployeeID,
    weekEndingDate = PayPeriod
}

The first two fail saying AND and && are not valid, the last fails saying leftth2 is not in the scope of the left side. 前两个失败说AND&&无效,最后一个失败说leftth2不在左侧范围内。

What is the proper way of doing this? 这样做的正确方法是什么?

The condition 条件

tch2.WeekEndingDate = @PayPeriod

doesn't feel like part of the join to me - it's not comparing data in two different rows. 不喜欢加入我的一部分 - 它不是比较两个不同行中的数据。 It feels like it should be part of what you're joining on , leading to: 这感觉就像它应该是你要加入什么 ,导致部分:

join leftth2 in db.TimeCardHeaders.Where(p => p.WeekEndingDate == PayPeriod)
    on th1.EmployeeID equals leftth2.EmployeeID into leftjointh2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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