开发者

Converting IGrouping<T,U> to IGrouping<T,V> using LINQ

开发者 https://www.devze.com 2022-12-25 16:14 出处:网络
开发者_Python百科How can I do this conversion?Is it possible with some simple LINQ query?If V is some other type not involved in the query, you can use the let keyword to create an instance and then g
开发者_Python百科

How can I do this conversion? Is it possible with some simple LINQ query?


If V is some other type not involved in the query, you can use the let keyword to create an instance and then group on it...

from x in Y
let v = new V(x.Whatever)
group v by v.Whatever into vGroup
select vGroup


Assuming that V inherits from U and you want to cast each U to a V :

IEnumerable<IGrouping<string, U>> groupingsOfU =
    from u in listOfU
    group u by u.Foo;

IEnumerable<IGrouping<string, V>> groupingsOfV =
    from g in groupingsOfU
    from u in g
    group (V)u by g.Key;
0

精彩评论

暂无评论...
验证码 换一张
取 消