我如何在LINQ中表达以下内容?
select * from table_1 t1
join table_2 t2 on t1.col1 = t2.col1 and t1.col2 <= t2.col2
这是我尝试过的,但我没有得到预期的结果。我想加入
2个表但由于table_1
数量不大于或等于table_2
WidgetA
数量的 List<anObj> table_1 = new List<anObj>();
table_1.Add(new anObj() { Product = "WidgetA", Quantity = 0 });
table_1.Add(new anObj() { Product = "WidgetB", Quantity = 3 });
List<anObj> table_2 = new List<anObj>();
table_2.Add(new anObj() { Product = "WidgetA", Quantity = 1 });
table_2.Add(new anObj() { Product = "WidgetB", Quantity = 1 });
var xList =
from t1 in table_1
join t2 in table_2
on t1.Product equals t2.Product
where t1.Quantity >= t2.Quantity
select new
{
t1,
t2
};
if (xList.Any())
Console.WriteLine("Found");
else
Console.WriteLine("None"); //I'm looking for this result.
数量
我不希望结果中有任何记录(在xList中)。
{{1}}
答案 0 :(得分:4)
from t1 in t1datatable
join t2 in t2datatable
on t1.col1 equals t2.col1
where t1.col2 <= t2.col2
select new
{
t1,
t2
};
答案 1 :(得分:2)
最简单的方法是编写以下内容:
var q = from t1 in table_1
from t2 in table_2.Where(x => t1.col1 == x.col1 && t1.col2 <= x.col2)
select new { t1, t2 };
(即使它看起来如此,也不会在数据库中创建交叉连接执行计划)
答案 2 :(得分:1)
var result = from t1 in db.table_1
from t2 in db.table_2.Where(x => t1.col1 == x.col1)
.Where(x => t1.col2 <= x.col2)
select new {t1, t2};