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?
In this case you have to use
varbecause you have an anonymous type. This situation is in fact why it was necessary to addvarto the language. If you want to write an explicit type instead ofvarthen you have to select a concrete class which must be defined somewhere. Then your code can look like this:I suspect though that the above query is not correct. You perform a grouping but then you only use
grouped.Key.