I need to group the table according to the Month in the table.
The query goes this way:
var query =
from a in db.Dates
from b in db.Facts
where a.Count_Key == b.Date_key
select new
{
a.Month,
b.Fact_key
};
From this query I try to group by Month
query = query.GroupBy(x => x.Month);
Grid1.DataSource = query;
Grid1.DataBind();
Then I get the following error which says:
Cannot implicitly convert IGrouping int ? into IQueryable
You can't reassign back to the query variable as the results of those two queries are different types.
Your first query is essentially this:
IQueryable<'a> query =
from a in db.Dates
from b in db.Facts
where a.Count_Key == b.Date_key
select new
{
a.Month,
b.Fact_key
};
Ie it returns a straight IQueryable
of the anonymous type.
Your second query returns an IQueryable<IGrouping<'a>>
, ie an IQueryable
of a group of the anonymous type:
IQueryable<IGrouping<'a>> groupedQuery = query.GroupBy(x => x.Month);
Therefore because the return types are different you can't assign the result of the grouping back to the original variable.
You can do this:
var query2 = query.GroupBy(x => x.Month);
Grid1.DataSource = query2;
Grid1.DataBind();
The problem is caused because the var query
is implicitly inferred from its usage. The query
is of type IQueryable<SomeAnonymousType>
and the GroupBy
method returns an IQueryable<IGrouping<int?, SomeAnonymousType>>
. Those are simply two different types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.