开发者

LINQ Having Clause with inner select

开发者 https://www.devze.com 2023-01-08 07:21 出处:网络
How would the following be converted to LINQ-EF select Name开发者_StackOverflow社区 from TableA as TableAOuter

How would the following be converted to LINQ-EF

select Name开发者_StackOverflow社区
from TableA as TableAOuter
group by TableAOuter.Name, TableAOuter.Id
having(
    select count(TableAInner.Id)
    from TableA as TableAInner
    where TAbleAInner.Reference=TableAOuter.Id) = 0
);


To me, that looks like:

var query = from row in tableOuter
            group row by new { row.Name, row.Id } into g
            where !tableInner.Any(inner => inner.Reference == g.Key.Id)
            select g.Key.Name;

Although I'd be tempted to perform the filtering before the grouping - at which point you can just group the row name:

var query = from row in tableOuter
            where !tableInner.Any(row => inner.Reference == row.Id)
            group row.Name by new { row.Name, row.Id };

I think that should do the same thing, right?

0

精彩评论

暂无评论...
验证码 换一张
取 消