开发者

linq select distinct date from a DataTable

开发者 https://www.devze.com 2023-03-29 16:56 出处:网络
I want to fetch distinct date from a DataTable. Currently I\'m using the below code: MyDataTable.DefaultView.ToTable(true, \"MyDateColumn\");

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 Selecting the column by just the Date property of the column:

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

精彩评论

暂无评论...
验证码 换一张
取 消