IGrouping and Casting in Linq

Posted by FiveTools on Stack Overflow See other posts from Stack Overflow or by FiveTools
Published on 2010-05-06T18:42:03Z Indexed on 2010/05/06 18:58 UTC
Read the original article Hit count: 274

Filed under:
|
|

I have the following query:

var groupCats =
            from g in groups
            group g by g.Value into grouped
            select new
            {
                GroupCategory = grouped.Key,
                Categories = GetCategories(grouped.Key, child)
            };

This works fine. In the anonymous type returned GroupCategory is a string, and Categories are an Enumerable - what is the proper way to declare this instead of using 'var'?

I tried:

IGrouping<string,string> groupCats =
            from g in groups
            group g by g.Value into grouped
            select new
            {
                GroupCategory = grouped.Key,
                Categories = GetCategories(grouped.Key, child)
            };

and

IGrouping<string,Enumerable<string>> groupCats =
            from g in groups
            group g by g.Value into grouped
            select new
            {
                GroupCategory = grouped.Key,
                Categories = GetCategories(grouped.Key, child)
            };

In both instances I get:

Cannot implicity convert type....An explicit conversion exists (are you missing a cast)

How do I cast this?

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about grouping