c# 4.0 - How to access anonymous type projected in Linq query in the next chained extension method? -
i have collection of schemas, each schema has name, id , several other properties. need project name , id of every schema , creating anonymous type in linq query follows:
from s in db.schemas select new { id = s.schemaid, name = s.name} i need convert above anonymous type projected via select query operator dictionary<int,string> , need add further extension method overload above query follows:
(from s in db.schemas select new { id = s.schemaid, name = s.name}) .todictionary<tsource,int,string>() now, how can access anonymous type in todictionary() overload?
what should replace tsource type parameter with?
i have other methods this, wondering if can done in way accessing anonymous type. other nice method welcome.
you can elide type arguments completely.
(from s in db.schemas select new { id = s.schemaid, name = s.name}) .todictionary(p => p.id, p => p.name); the compiler can infer them you.
in fact, may able in single step:
db.schemas.todictionary(s => s.schemaid, s => s.name); or @ least sticking 1 type of linq syntax:
db.schemas .select(s => new { id = s.schemaid, name = s.name }) .todictionary(p => p.id, p => p.name);
Comments
Post a Comment