LINQ在哪里加入条款

编程入门 行业动态 更新时间:2024-10-25 04:15:39
本文介绍了LINQ在哪里加入条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用join/where子句和一个相当简单的sql select语句苦苦挣扎.

I'm struggling with a join/where clause with what is a rather simple sql select statement.

我正在尝试从tb1检索产品信息列表,其中where条件位于tbl2中,但这必须由三个不同的列连接起来.

I am trying to retrieve a list of product information from tb1 with the where condition behind situated in tbl2 but this must be joined by three different columns.

因此SQL看起来类似于:

so the SQL would look something along the lines of:

SELECT tb1.* FROM tb2 INNER JOIN tb1 ON tb2.Col1 = tb1. Col1 AND tb2.Col2 = tb1. Col2 AND tb2.Col3 = tb1.Col3 WHERE (tb2.Col1 = col1) AND (tb2.Col2 = col2) AND (tb2.Col4 = string)

ColX是主要的where子句,带有要作为参数传递的字符串;所有其他列都在上下文中.

ColX is the main where clause with the string to be passed in as parameter; all other columns are within the contexts.

如何使用where子句实现多个联接?

How do you implement multiple joins with a where clause?

朝正确的方向推,非常感谢.

And shoves in the right direction, muchly appreciated.

推荐答案

要在LINQ中的多个字段上进行联接,您必须创建一个包含要比较的列的新匿名类型,然后在该联接中使用该匿名类型:

To join on multiple field in LINQ, you have to create a new anonymous type containing the columns you want to compare and then use that anonymous type in the join:

var results = from t1 in context.tb1 join t2 in context.tb2 on new { t1.Col1, t1.Col2, t1.Col3 } equals new { t2.Col1, t2.Col2, t2.Col3 } where t2.Col1 == col1 && t2.Col2 == col2 && t2.Col4 == someString select t1;

这是等效的Lambda语法:

And here is the equivalent Lambda Syntax:

var results = context.tb1.Join( context.tb2, t1 => new { t1.Col1, t1.Col2, t1.Col3 }, t2 => new { t2.Col1, t2.Col2, t2.Col3 }, (t1, t2) => new { t1, t2 }) .Where(o => o.t2.Col1 == col1 && o.t2.Col2 == col2 && o.t2.Col4 == someString) .Select(o => o.t1);

如您所见,对于联接,查询语法通常会产生易于阅读的语句.

As you can see, in the case of joins, query syntax usually produces an easier to read statement.

更多推荐

LINQ在哪里加入条款

本文发布于:2023-11-16 21:58:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条款   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!