Click here to Skip to main content
16,018,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello CPians,

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

The code is like below...
C#
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);


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
Comments
walterhevedeich 23-May-13 3:16am    
Did you try checking the contents of slotsAvailable even before the group? Maybe those dates were already in there in the first place.
Yes, I have checked.
Initially they are not present, but after I group them, they are getting added.

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.
C#
// 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();
 
Share this answer
 
Comments
digi0ps 31-May-13 0:49am    
Sorry Dash, I dont know LinQ. Am only web dev! :)
Ok. So what you want to know here ?
This was my question and finally I came with a solution, so I have added that.

Do you have doubts here ? Anything to understand ?
digi0ps 31-May-13 5:57am    
No. I said i dont know LinQ.
Ok. Fine... :)
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.

C#
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;}
}



A quick and dirty fix for this would be to add a .Where to your .GroupBy that says Where Date != "1/1/0001".
C#
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());   
   }
}
 
Share this answer
 
v3
Comments
Hi David,

Thanks for the reply.

I was also suspecting the same thing while I got the problem. That is missing Dates in some Slots. But at that time, I found that all the slots have the same no of DateTimes and all are valid ones. And every list element have same no of DateTimes.

So, I don't know what is the issue. #DrivesMeCrazy.

Therefore, I was also came up with a solution as you just suggested (to remove the null ones), just could not posted the solution here. I will post that solution now and I am also accepting your's.

Thanks a lot for taking interest in my question.

Regards,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900