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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With