简体   繁体   English

SQL到SQL连接语句中的'AND'将Linq左连接

[英]SQL to Linq Left Join with 'AND' in the SQL Join Statement

I have a SQL query that works perfectly that I need to convert to Linq. 我有一个可以完美转换为Linq的SQL查询。 I need to return all the records one table and join it to a second table. 我需要将所有记录返回到一个表并将其连接到第二个表。 I need to return all of the results from the first table joined with results from the second table where the value of a specific field in the second table equals a variable value (75 in the example below) or returns null. 我需要返回第一个表中的所有结果以及第二个表中的结果,其中第二个表中特定字段的值等于变量值(在下面的示例中为75)或返回null。

So the total number of rows in the result should be the total number of rows from table1. 因此,结果中的总行数应为table1中的总行数。 The part of the row from the join from table2 should either show values from table2 where a record existed with a value of 75 or null where the record doesn't exist. 来自table2的联接的那部分行应该显示table2的值(其中存在记录的值为75),或者显示null(不存在该记录的位置)。

EDIT: I should mention that t1.field1 is an int and t2.field1 is a nullable int. 编辑:我应该提到t1.field1是一个int和t2.field1是可为空的int。

I tried multiple linq statements, grouping joins, asking coworkers, and googling til my eyes bleed. 我尝试了多个linq语句,对联接进行分组,询问同事以及在眼睛流血之前一直使用Google搜索。 I'm stuck. 我被卡住了。 I realize my question wording may not be clear, and I apologize in advance if it isn't. 我意识到我的问题措词可能不清楚,如果不清楚,我会提前道歉。

Thanks in advance. 提前致谢。 Chris 克里斯

The SQL Query: SQL查询:

SELECT *
FROM table1 AS t1 LEFT OUTER JOIN 
table2 AS t2 ON t1.field1 = t2.field1 AND t2.field2 = 75

Use DefaultIfEmpty - see LINQ - Left Join, Group By, and Count and http://msdn.microsoft.com/en-us/library/bb397895.aspx for samples of how to achieve this 使用DefaultIfEmpty请参阅LINQ-左联接,分组依据和计数以及http://msdn.microsoft.com/zh-cn/library/bb397895.aspx,以获取有关如何实现此目的的示例

An answer that works but isn't as elegant as I'd expected: 一个有效但不如我预期的答案:

var q = from item1 in table1  
            join item2 in table2 on new { Field1=(int?)item1.Field1, Field2=75 }  
                   equals new { item2.Field1, item2.Field2 } into gj  
            from subItem2 in gj.DefaultIfEmpty()  
            select new { F1= item1.Field1, F2 = ( subItem2 == null ? string.Empty : subItem2.Field2.ToString()) };

I couldn't see where to fit the where clause in (Field2 = 75) so instead went with the composite key between the tables to achieve the same effect. 我看不到在哪里适合(Field2 = 75)中的where子句,因此改为使用表之间的复合键来实现相同的效果。

Second bit of ugliness is the cast to the nullable int because or Field1 in the composite key so it can be equated to the corresponding field on table 2. 丑陋的第二位强制转换为可为null的int,因为复合键中的或Field1使其可以等于表2上的对应字段。

Obviously you return in the anonymous type whatever values you're interested in. Note you can't return null which is why I showed a string representation of Field2 just so you can see what is returned from the second table. 显然,您返回的匿名类型是您想要的任何值。请注意,您不能返回null,这就是为什么我显示Field2的字符串表示形式以便您可以看到第二个表返回的原因。

Thank you for your responses. 谢谢你的回复。 Because I need some nulls, but only the ones where the specific id is null and not all values that are null, none of the solutions above will work. 因为我需要一些空值,但只需要特定id为空的值,而不是所有值都为空的值,上述任何解决方案都无法使用。 It seems that to do this in linq will be very tough if it is possible. 如果可能,在linq中执行此操作似乎非常困难。

So, in the interest of time, I decided to take the SQL query that worked and turn it into a stored procedure and function import. 因此,为了节省时间,我决定采用有效的SQL查询并将其转换为存储过程和函数导入。 I feel like that's probably not the right way to do it, but time is always a factor. 我觉得这可能不是正确的方法,但是时间始终是一个因素。

Thanks again for your responses. 再次感谢您的回复。

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

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