使用LINQ按日期列出组日期时间列表 [英] Group DateTime List By Date using LINQ

查看:93
本文介绍了使用LINQ按日期列出组日期时间列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好CPians,



我正在分享这个问题,我在分组 DateTime 列表时遇到了这个问题通过日期



代码如下所示......

Hello CPians,

I am sharing the problem, which I am facing while grouping the DateTime List by Date.

The code is like below...

List<DateTime> slotsAvailable = new List<DateTime>();

// Then I did some operations and added some DateTime to this list and now sorting it.
slotsAvailable.Sort();

// Here I am Grouping the List.
var grouppedResult = slotsAvailable.GroupBy(x => x.Date);





问题

当我按日期对DateTime列表进行分组时,一个组最多可包含18个DateTime是否。



例如 - DateTime 就像18/05 / 2013 11:00:00 AM....18/05/2013 07:30:00 PM

现在,当我按日期分组时,18/05/2013​​,那么该小组应该包含18个元素,从18/05/2013 11:00:00 AM,18/05/2013 11:30开始:00 AMup to18/05/2013 07:30:00 PM,间隔 30 分钟。



但我在第18个元素之后添加了许多默认 DateTime 添加到该组。



您可以在图像中看到 - 使用LINQ按日期列出日期时间组 [ ^ ],我在其中添加了调试结果。



在图像中,您可以检查元素组中[17]位置之后是否有许多默认 DateTime 添加如01/01 / 0001 12:00:00 AM



请解释wh at是问题以及如何解决它。



谢谢,

Tadit



Problem
When I am grouping the DateTime List by Date, one Group can contain maximum 18 number of DateTimes.

For Example - DateTime are like "18/05/2013 11:00:00 AM" .... "18/05/2013 07:30:00 PM".
Now, when I group by Date that is by "18/05/2013", then the group should contain 18 number of elements starting from "18/05/2013 11:00:00 AM", "18/05/2013 11:30:00 AM" up to "18/05/2013 07:30:00 PM" with a interval of 30 minutes.

But I am getting many default DateTime added to this group after the 18th element.

You can see in the image - Group DateTime List By Date using LINQ[^], where I added the debugged result.

In the image, you can check that after the [17] position in the elements group, there are many default DateTime added like "01/01/0001 12:00:00 AM".

Please explain what is the problem and how to solve it.

Thanks,
Tadit

推荐答案

嘿Tadit,



从我所知道的,你在你的收藏中获得null日期时间的原因是因为你的插槽集合中的任何类别都可以使用是...没有那些日期时间的值。



这是重新解决你的问题......通过输出到Console.WriteLine来管理组。 />


Hey Tadit,

From what i can tell, the reason why your getting "null" date times in your collection is because whatever class your collection of slotsAvailable is...does not have values for those datetimes.

Here is a recreation of your problem...piping the group by output to Console.WriteLine.

public static void Q594720()
{
   List<Slots> slotsAvailable = new List<Slots>();
   slotsAvailable.Add(new Slots { Name = "Class 1", Date = DateTime.Now.AddDays(1) });
   slotsAvailable.Add(new Slots { Name = "Class 2", Date = DateTime.Now.AddDays(2) });
   slotsAvailable.Add(new Slots { Name = "Class 3", Date = DateTime.Now.AddDays(3) });
   slotsAvailable.Add(new Slots { Name = "Class 4", Date = DateTime.Now.AddDays(4) });
   slotsAvailable.Add(new Slots { Name = "Class 5" });
   slotsAvailable.Add(new Slots { Name = "Class 6" });
   slotsAvailable.Add(new Slots { Name = "Class 7" });
   slotsAvailable.Add(new Slots { Name = "Class 8" });


   foreach (var items in slotsAvailable.GroupBy(m => m.Date).ToList())
   {
      foreach (var slotse in items)
      {
         Console.WriteLine("Date Value:" + slotse.Date.ToString());   
      }
   }
}

public class Slots
{
   public string Name {get;set;}
   public DateTime Date {get;set;}
}







对此进行快速而肮脏的修复是在你的.GroupBy中添加一个.Where,其中说明Where Date!=1/1/0001 。




A quick and dirty fix for this would be to add a .Where to your .GroupBy that says Where Date != "1/1/0001".

foreach (var items in slotsAvailable.Where(m=>m.Date.ToShortDateString() != "1/1/0001").GroupBy(m => m.Date).ToList())
{
   foreach (var slotse in items)
   {
      Console.WriteLine("Date Value:" + slotse.Date.ToString());   
   }
}


最后我想出了解决方案中 @David 建议的类似解决方案1删除null。在他给出解决方案后立即发布。



他正在比较如下。

m.Date.ToShortDateString ()!=1/1/0001



我与 DateTime.MinValue 。所以,我的代码如下所示。

Finally I came up with a similar solution as suggested by @David in Solution 1 to remove the null ones. Posting it now after he gave the solution.

He is comparing like below.
m.Date.ToShortDateString() != "1/1/0001"

I compared with DateTime.MinValue. So, my code is like below.
// Other logic to Remove the duplicate DateTimes.
var distinctResult = slotsAvailable.GroupBy(x => x.ToString()).Select(y => y.First()).ToList();

distinctResult.Sort();

// Logic to Group By Date.
var distinctGroupedResultByDate = distinctResult.GroupBy(x => x.Date);

// Remove the null or <code>DateTime</code> which has <code>MinValue</code>.
var finalAvailableSlots = from q in distinctGroupedResultByDate
                          where q.ToString() != DateTime.MinValue.ToString()
                          select q.ToList();


这篇关于使用LINQ按日期列出组日期时间列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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