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 t开发者_JS百科he 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).
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();
精彩评论