简体   繁体   English

Linq查询有group by

[英]Linq query having group by

RowNum IMAGEID     SCANEDATE                COUNT
1      10000131    2012-07-04 00:00:00.000  1
2      10002626    2012-08-03 00:00:00.000  1
3      10003348    2012-09-06 00:00:00.000  1
4      10003589    2012-09-15 00:00:00.000  8
5      10003590    2012-05-15 00:00:00.000  8
6      10003591    2012-04-15 00:00:00.000  8
7      10003592    2012-03-15 00:00:00.000  8
8      10003595    2012-02-15 00:00:00.000  8
9      10003596    2012-09-15 00:00:00.000  8
10     10003598    2012-09-15 00:00:00.000  8
11     10003599    2012-09-15 00:00:00.000  8

I have above datatable on which I need to apply linq query for getting result as follows 我有上面的数据表,我需要应用linq查询获取结果如下

1) All images ids having same date should be listed in same cell(, seperated), and the count column should have the count of each date's respective no. 1)具有相同日期的所有图像ID应列在同一单元格中(分隔),并且计数列应具有每个日期的相应编号。 of image ids 图像ID

 Scan Date   Image ID  Count


      11/27/2007 1001529,1001530,1001531,1001532,1001533,1001534,1001537,1001538,1001539,1001540,1001542    11

      11/20/2008 1002501,1002502,1002503,1002504,1002505,1002506,1002507,1002508,1002509,1002510,1002511,1002512,1002513,1002514,1002515,1002516,1002517,1002518,1002519,1002520,    20

      7/5/2011   1015237,1015238    2

      7/6/2011   1015248,1015249,1015259,1015260,1015286,1015287,1015288,1015289,1015290,1015291,1015292,1015293,1015294,1015295,1015296,1015297,1015347,1015348,1015358,1015359,    32
      1015370,1015371,1015381,1015382,1015396,1015397,1015410,1015411,1015412,1015413,1015429,1015430

      7/7/2011   1015444,1015445    2

Please provide me the query for doing the above. 请向我提供上述操作的查询。

Group table rows by SCANEDATE field. 按SCANEDATE字段对表行进行分组。 Then project each group of rows by selecting scan date (key of group), joining all images ids into string, and getting count of rows in a group: 然后通过选择扫描日期(组的键),将所有图像ID连接到字符串,以及获取组中的行数来计划每组行:

table.AsEnumerable()
     .GroupBy(r => r.Field<DateTime>("SCANEDATE"))
     .Select(g => new {
          ScanDate = g.Key,
          Ids = String.Join(",", g.Select(r => r.Field<int>("IIMAGEID"))),
          Count = g.Count()
      });
var results = from p in tabel
              group p.IMAGEID  by p.SCANEDATE into g
              select new { SCANEDATE = g.Key, IMAGEID = g.ToList() };

Or as a non-query expression: 或者作为非查询表达式:

var results = tabel.GroupBy(p => p.SCANEDATE , p => p.IMAGEID ,
                         (key, g) => new { SCANEDATE = key, IMAGEID = g.ToList() });

Basically the contents of the group (when view as an IEnumerable<T> ) is a sequence of whatever values were in the projection (p.car in this case) present for the given key. 基本上,组的内容(当作为IEnumerable<T>查看时)是给定键存在的投影(在这种情况下为p.car)中的任何值的序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM