简体   繁体   English

LINQ和按日期范围分组数据

[英]LINQ & Grouping data by a date range

Say I have the below set of data where I wish to group the Description column based on whether the dates for matching Descriptions are within a few seconds of each other. 假设我有以下数据集,希望根据匹配描述的日期是否在几秒钟之内将“描述”列分组。

在此处输入图片说明

The output I'm looking for is the Description and the minimum Date for that group. 我正在寻找的输出是该组的描述和最小日期。 It is possible that the Description could be the same but the dates might be days different, in this case I would want two outputted rows. 描述可能相同,但日期可能不同,在这种情况下,我需要两个输出行。

In the case above take a look at the Description "TEST s" where I would want two outputted grouped rows 在上述情况下,请查看描述“ TEST s”,在该处我想要两个输出的分组行

TEST s 2014-12-04 16:27:44.903 测试s 2014-12-04 16:27:44.903

TEST s 2014-12-04 17:21:21.233 测试2014-12-04 17:21:21.233

Is this possible using Linq? 使用Linq可以吗?

Try this: 尝试这个:

var q = from item in lstMyTable
        group item by item.ItemDate.ToString("MM/dd/yyyy HH:mm") into ItemGroup
        select new
        {
            Description = ItemGroup.First().Description,
            Date = ItemGroup.OrderBy(x => x.ItemDate).First().ItemDate
        };

The above linq query groups by date + hour + minutes, ignoring seconds. 上面的linq查询按日期+小时+分钟分组,忽略秒。 The OrderBy applied to ItemGroup ensures that we get the mininum date, as described in the OP. 如OP中所述,应用于ItemGroupOrderBy可确保我们获得最短日期。

With this input: 使用此输入:

List<MyTable> lstMyTable = new List<MyTable>()
{
  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 16, 27, 11) },
  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 16, 27, 12) },
  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 16, 27, 13) },

  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 17, 21, 11) },
  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 17, 21, 12) },
  new MyTable() { Description = "TEST s", ItemDate = new DateTime(2014, 12, 4, 17, 21, 13) }
};

you get this result: 您得到以下结果:

[0] = { Description = "TEST s", Date = {4/12/2014 4:27:11 pm} }
[1] = { Description = "TEST s", Date = {4/12/2014 5:21:11 pm} }

If you want to also group by description then simply substitue the group by clause with sth like this: 如果您也想按描述分组,则只需用sth代替group by子句,如下所示:

group item by new 
{ 
   a = item.ItemDate.ToString("MM/dd/yyyy HH:mm") ,
   b = item.Description 
} into ItemGroup

Finally, by converting the Datetime field to Ticks , you can fiddle with the required time distance between separate records, eg 最后,通过将Datetime字段转换为Ticks ,您可以弄乱各个记录之间所需的时间距离,例如

group item by new 
{ 
    a = item.ItemDate.Ticks.ToString().Substring(0, item.ItemDate.Ticks.ToString().Length - 10), // instead of .ToString("MM/dd/yyyy HH:mm") ,
    b = item.Description 
} into ItemGroup

You can play around by varying the number of digits subtracted from item.ItemDate.Ticks , substituting eg 10 with 9 or 8, etc. 您可以通过改变从item.ItemDate.Ticks减去的位数来item.ItemDate.Ticks ,例如用10替换9或8,等等。

If you want left hand side to be a list then you can try this 如果您希望左侧为列表,则可以尝试此操作

var query = (from t in db.Table
             group t by new {t.Description, t.Date }
             into grp
             select new
             {
                 grp.Key.Description,
                 grp.Key.Date,                        
             }).ToList();

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

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