c# - linq: Does ToDictionary cut down the number of columns returned? -
i have simple linq query:
var dict = (from in customers select i).todictionary(i => i.id1, => i.id2); does linq2sql know cut down select 2 fields (id1, id2) although query says select i?
no. queryable class things linq sql rely on doesn't contain declaration todictionary (and there no other classes suitable extension methods) ends using implementation in enumerable - i.e. linq objects. uses plain delegate rather expression tree, there's no sensible way could work out what's required.
to make query more efficient, use anonymous type capture you're going need in select part:
var dict = db.customers // here full customer, logically... .select(i => new { i.id1, i.id2 }) // here p pair of id1, id2 .todictionary(p => p.id1, p => p.id2); another alternative extension method - should possible write overload of todictionary taking 2 expression trees (and query far), , combine them new expression tree create key/value pair, pass select, calling normal todictionary afterwards. it's not worth hassle if you're doing once, if you're doing in multiple places useful.
Comments
Post a Comment