Linq-加入混乱 [英] Linq - Join Confusion
问题描述
出于这个问题,我们假设我有表'A'和'B',并且它们之间存在1:1的关系.在Linq查询中,我可以执行以下操作:
For the sake of this question, let's assume I have tables 'A' and 'B' and there is a 1:1 relationship between them. In a Linq query, I can do the following:
from row in A
where row.B.Description = someValue
select A
其中row.B是对表'B'的引用.
Where row.B is the reference to table 'B'.
但是,现在让我们假设"A"和"B"之间存在1:M的关系.上面的查询不再起作用.看来我需要显式使用"join",如下所示:
However, let's now assume there is a 1:M relationship between 'A' and 'B'. The above query no longer works. It seems I need to explicitly use 'join' as follows:
from row in A
join row1 in B on row.BId = row1.BId
where row1.Description = someValue
select A
我的问题是这个.对于1:M关系,我是否正确要求加入"?还是有办法在不使用联接的情况下(例如1:1)执行此查询?
My question is this. Am I correct that for 1:M relationships, 'join' is required? Or is there are way to do this query, without using join, like in the 1:1 case?
推荐答案
您不必显式加入,选择很多就可以解决问题
You don't have to join explicitly, select many will do the trick
from row in A
from row1 in row.B
where row1.Description == someValue
select row
或者(虽然我真的不喜欢)
alternatively (although I really don't like it)
from row in A
where row.B.Any(b => b.Description == someValue)
select row
使用第一个选项时,如果有多个具有相同描述的B,则需要对结果执行Distinct().
With the first option you will need to do a Distinct() on the result if there are many B's that have the same description.
这篇关于Linq-加入混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!