Reputation: 5801
I have the following code:
List<DateTime> dates = new List<DateTime>();
//filling the list somehow
dates = dates.Distinct().ToList();
The code is basically working but I get the list of unique DateTime. And I want to get the list of unique dates since I don't need times at all here. Is there a nice solution?
My question differs from
c# Linq select distinct date time days
since I am not quering the database actually. I just want to filter the list.
Upvotes: 12
Views: 11396
Reputation: 31
I am sure that you can use construction like:
List<DateTime> dates = new List<DateTime>();
//filling the list somehow
List<DateTime> dates2 = new List<DateTime> ();
foreach (var v in dates) {
if (!dates2.Contains (v.Date)) {
dates2.Add (v.Date);
}
}
dates = dates2;
but there can be better option for this.
Upvotes: 2
Reputation: 166376
How about using a transform SELECT?
Projects each element of a sequence into a new form.
Something like
dates = dates.Select(x => x.Date).Distinct().ToList();
Upvotes: 38