试图LINQ使用GROUPBY时电网的问题 [英] Problem with grid when trying to use groupby in LINQ

查看:101
本文介绍了试图LINQ使用GROUPBY时电网的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用GROUPBY我得到一个错误说一个字段日期1上没有选择的资源发现。

When trying to use GROUPBY I get an error saying a field 'date1' is not found on selected resource.

   var query = (from a in db.Dates
                     from b in db.Facts

                     where a.Count_Key == b.Date_key

                         select new{
                             a.Date1,
                           a.Month,
                         b.Fact_key
                         });
        var query2 = query.GroupBy(x =>  x.Month );
        Grid1.DataSource = query2;
        Grid1.DataBind();

所以,当我尝试使用查询它完美结合,但QUERY2产生误差

So, when I try to bind with query it works perfectly, but query2 yields the error

现场DATE1不选择数据源中。

field date1 not found on selected datasource.

我该如何解决这个问题?

How can I fix this?

推荐答案

由于您按月分组,现在你只有一个月领域作为重点,并分组的项目的集合。如果您想对数据源的多个字段,你需要在日期1字段中使用聚合函数。

Because you grouped by month, now you have only month field as a key, and collection of grouped items. If you want more fields on data source you need to use aggregate function on date1 field.

例如这样的:

var query2 = (from q in query
             group q by q.Month into g
             select new 
                       {
                          Month = g.Key,
                          Date = g.Select(gg=>gg.Date1).Max //or you can use first here etc.
                       }).ToList()

希望这有助于

这篇关于试图LINQ使用GROUPBY时电网的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆