linq group by multiple aggregate

时间:2012-11-01 13:40:19

标签: sql linq

我是Linq的新手,我确信我已经以复杂的方式解决了这个问题。我试图在Linq中做这样的SQL:

SELECT DISTINCT
   count(vendor) as vendorCount,
   reqDate,
   status,
   openDate,
   item,
   poDate,
   count(responseDate) as responseCount
FROM
   myTable
GROUP BY
   reqDate, status, openDate, item, poDate
HAVING
   reqDate > openDate

这是我到目前为止所拥有的。

        var groupQuery = (from table in dt.AsEnumerable()
                         group table by new
                         {                                 
                             vendor = table["vendor"], reqdate = table.Field<DateTime>("ReqDate"), status = table["status"],
                             open = table["openDate"],  
                             item = table["item"), 
                             podate = table.Field<DateTime>("PODate"), responsedate =  table.Field<DateTime>("responseDate"), 
                         }
                             into groupedTable
                             where Having(groupedTable.Key.reqdate, groupedTable.Key.openDate) == 1
                             select new
                             {
                                 x = groupedTable.Key,  
                                 y = groupedTable.Count()
                             }).Distinct();



        foreach (var req in groupQuery)
        {
            Console.WriteLine("cols: {0} count: {1} ",
                req.x, req.y);
        }

var groupQuery = (from table in dt.AsEnumerable() group table by new { vendor = table["vendor"], reqdate = table.Field<DateTime>("ReqDate"), status = table["status"], open = table["openDate"], item = table["item"), podate = table.Field<DateTime>("PODate"), responsedate = table.Field<DateTime>("responseDate"), } into groupedTable where Having(groupedTable.Key.reqdate, groupedTable.Key.openDate) == 1 select new { x = groupedTable.Key, y = groupedTable.Count() }).Distinct(); foreach (var req in groupQuery) { Console.WriteLine("cols: {0} count: {1} ", req.x, req.y); }

Having()是一个函数,它接受两个日期时间参数,如果reqDate大于openDate则返回1。它编译并运行,但它显然不能给我我想要的结果。这可能使用Linq吗?我想将这些数据推送到excel电子表格,所以我希望从这个linq查询创建一个数据表。我最好只从我的数据表创建一个数据视图而不是弄乱Linq?

1 个答案:

答案 0 :(得分:1)

SQL代码仅按部分字段进行分组,而LINQ语句按所有字段进行分组,因此唯一可以分组的项目是重复项。如果仅按SQL查询分组的字段进行分组,则应获得正确的答案。您的Having()方法字很好,但没有必要且可读性较差。

var groupQuery = (from table in dt.AsEnumerable()
                 group table by new
                 {                                 
                     reqdate = table.Field<DateTime>("ReqDate"),
                     status = table["status"],
                     open = table["openDate"],  
                     item = table["item"), 
                     podate = table.Field<DateTime>("PODate")
                 }
                 into groupedTable
                 where groupedTable.Key.reqdate > groupedTable.Key.openDate
                 select new
                 {
                     x = groupedTable.Key,  
                     VenderCount = groupedTable.Select(t => t["vendor"])
                                               .Distinct()
                                               .Count(),
                     ResponseCount = groupedTable.Select(t => t.Field<DateTime>("responseDate"))
                                                 .Distinct()
                                                 .Count()
                 }).Distinct();