views:

49

answers:

1

How would the following be converted to LINQ-EF

select Name
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
);
+1  A: 

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?

Jon Skeet
Shouldn't it be `!tableInner.Any(inner => inner.Reference == row.Id)` and `group row.Name by new`?
Filip Ekberg
@Jon, thanks, works perfectly with the changes I commented above!
Filip Ekberg
Whoops - I meant "group row by" in both cases. In the first version you can't use `row.Id` in the where clause because it's not in scope. Fixed both and simplified the latter approach.
Jon Skeet
Thanks, it works perfectly.
Filip Ekberg