简体   繁体   中英

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(ie by ignoring time) .

You can try Select ing the column by just the Date property of the column:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM