c# - Convert a two-step LINQ query into a single LET query? -
i have following code:
var data = /* enumerable containing rows in table */; var part = "x"; var items = new hashset<int>(data .where(x => x.partname == part) .select(x => x.itemname)); var query = data.where(x => items.contains(x.itemname));
items
finds items in data specific partname
.
query
returns records in data item.
i need convert single let statement (or else) can use embedded in larger linq query.
for example, if data follows (records):
itemname partname 1 1 b 2 3 c
and looking items part "a" need end result be:
1 1 b 2
one suggestion this:
let = data.groupby(x => x.itemname) .where(g => g.any(x => x.partname == part)) .select(g => new { itemname = g.key, partnames = g.select(x => x.partname) })
however data returned in (let a) doesn't match expected results, returns like:
1 a,b // list of parts item 2
and need keep same pre-existing structure shown above.
any appreciated.
sound need selectmany
group:
data.groupby(x => x.itemname) .where(g => g.any(x => x.partname == part)) .selectmany(g => g);
Comments
Post a Comment