Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Datatable need to queried and fetch last modified date record with uniqueID using linq.

Input:

C#
id        Name     Depart     ModifiedDate
S123      vin       cse        2018-06-29 13:24:00.723
S124      sham      EEE        2018-06-29 13:24:00.724
S123      Vin       ECE        2018-06-29 13:25:00.000


I need output as


C#
id        Name     Depart     ModifiedDate
S123      Vin       ECE        2018-06-29 13:25:00.000
S124      sham      EEE        2018-06-29 13:24:00.724


What I have tried:

I need output as


C#
id        Name     Depart     ModifiedDate
S123      Vin       ECE        2018-06-29 13:25:00.000
S124      sham      EEE        2018-06-29 13:24:00.724
Posted
Updated 30-Jun-18 22:52pm

 
Share this answer
 
In addition to the Solution #1,

LINQ has OrderByDescending and Distinct methods. As you want unique records by Id, you first need to group the records by Id, apply distinct on it and then sort it.
var data = list.GroupBy(x => x.Id).Select(g => g.First()).ToList();
var datalist = data.Distinct().OrderByDescending(x => x.ModifiedDate);

This might solve your problem, give it a try!


KR
 
Share this answer
 

First of all, you need to covert your DataTable to an IEnumerable so that you can use linq. Then GroupBy the id column. Use the resultSelector function that's passed to the GroupBy statement to select an anonymous type that contains the most recent DateTime in the group. Use the values parameter and the OrderByDecending, Select and First statements to do this. In a lambda expression, you can reference a column field in a DataRow like this.

C#
dr.Field<string>("id")

If you get stuck, post your code and I will try to help you.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900