Select a Dictionary<T1, T2> with LINQ
Posted
by Rich
on Stack Overflow
See other posts from Stack Overflow
or by Rich
Published on 2009-03-06T00:20:24Z
Indexed on
2010/05/12
23:54 UTC
Read the original article
Hit count: 127
I have used the "select" keyword and extension method to return an IEnumerable<T>
with LINQ, but I have a need to return a generic Dictionary<T1, T2>
and can't figure it out. The example I learned this from used something in a form similar to the following:
IEnumerable<T> coll = from x in y
select new SomeClass{ prop1 = value1, prop2 = value2 };
I've also done the same thing with extension methods. I assumed that since the items in a Dictionary<T1, T2>
can be iterated as KeyValuePair<T1, T2>
that I could just replace "SomeClass" in the above example with "new KeyValuePair<T1, T2> { ...
", but that didn't work (Key and Value were marked as readonly, so I could not compile this code).
Is this possible, or do I need to do this in multiple steps?
Thanks.
© Stack Overflow or respective owner