Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of distinct dates

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.

like image 764
demonplus Avatar asked Apr 29 '15 11:04

demonplus


2 Answers

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();
like image 173
Adriaan Stander Avatar answered Oct 20 '22 22:10

Adriaan Stander


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.

like image 26
Kanzey Avatar answered Oct 20 '22 21:10

Kanzey