NaveenBhat
NaveenBhat

Reputation: 3328

linq select distinct date from a DataTable

I want to fetch distinct date from a DataTable. Currently I'm using the below code:

MyDataTable.DefaultView.ToTable(true, "MyDateColumn");

This code considers the time too but I don't want the time to be considered. I wrote the below code to select only Date Part but while making the distinct it considers the time too.

MyDataTable.AsEnumerable()
      .Select(row => new { MyDateColumn = row.Field<DateTime>("MyDateColumn").ToString("d") }).Distinct();

Please help me to select only distinct date(i.e by ignoring time).

Upvotes: 0

Views: 4464

Answers (1)

dlev
dlev

Reputation: 48596

You can try Selecting the column by just the Date property of the column:

MyDataTable.AsEnumerable()
      .Select(row => row.Field<DateTime>("MyDateColumn").Date).Distinct();

Upvotes: 2

Related Questions