Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Consider I am having a data structure

id Name Date
1 Alex 2-Apr-2013
2 Dale 3-Mar-2012
3 Alex 5-Apr-2013

I am having a class with similar data structure. My question is how would I extract the values with distinct name and if the name are similar then I have to get the one with the latest date.

For example the result has to be
id Name Date
3 Alex 5-Apr-2013 (=>Not the data with 2-Apr)
2 Dale 3-Mar-2012


I can get the distinct values by using IEqualityComparer but I don't know how to get the preferred data.??
Posted
Comments
[no name] 11-Feb-14 0:59am    
Have you has a look at this link?

http://msdn.microsoft.com/en-us/library/ms131190(v=vs.100).aspx
Prasaad SJ 11-Feb-14 1:29am    
Hi,
Thanks for your reply. I am quiet comfortable with using EqualityComparator. Buy my question is if 2 rows are equal then i have get the row with the latest date. How could that be possible
[no name] 11-Feb-14 4:05am    
so you got the answer and your question is how you got that answer, correct? that's what i understood from your last reply.if what i have understood was correct. you would have added the item in the list in such a way or you would have used some sorting logic before adding to that list. please correct me if i was wrong.

Please refer this code ...I created sample according to your requirement.

C#
public class SampleClass
    {
        public int ID { get; set; }
        public string  Name { get; set; }
        public DateTime Date { get; set; }

    }


Then following is query to get records:
XML
var sampleLst=new List<SampleClass>{
           new SampleClass{ ID=1,Name="Alex",Date=DateTime.Now},
           new SampleClass{ID=2,Name="Dale",Date=DateTime.Now},
           new SampleClass{ID=3,Name="Alex",Date=DateTime.Now.AddDays(1)}
        };
           var result = from t in sampleLst
                        group t by t.Name into g
                        select g.OrderByDescending(x=>x.Date).FirstOrDefault();
 
Share this answer
 
v2
Comments
johannesnestler 11-Feb-14 9:17am    
Good one. Maybe you could explain OP a Little better.
First Group by Name
then order Groups by date
last take first item of each Group
But 5ed anyway.
Prasaad SJ 18-Feb-14 3:29am    
Hi,
The above is just a example that I explained. What if I have 1000 rows and have 50 names with duplicate rows. The above will return only the first row. But my requirement is all the distinct rows has to be returned but the distinct row(the row which I get) has to be row with higher Date.
plz try this code.


////////sourceDt -datatable to be filtered
/////////Name,Date-table field
/////////resultDt-output datatable
////////assuming ur fields Name,Date,id fields on table are string datatype(VARCHAR)
C#
var distinctData = from c in sourceDt.AsEnumerable()
group c by c.Field<string>("Name") into grp
select grp.OrderByDescending(c => Convert.ToDateTime(c.Field<string>("Date")) ).FirstOrDefault();
DataTable resultDt = distinctData.CopyToDataTable();
 
Share this answer
 
v2
Comments
Prasaad SJ 11-Feb-14 4:24am    
Basically you are arranging the list in descending order and then you are using a distinct value. This is how I am using now and it works well as the first row in the duplicate rows returned everytime.

Thanks.
george4986 11-Feb-14 5:28am    
first grouping with name then sorting in descending date after that selecting first record
var query = (from user _db.Users select user).Distinct().OrderBy(u=>u.firstName).ToList();
 
Share this answer
 
Comments
Prasaad SJ 11-Feb-14 4:21am    
Hi Vipin,
This will return the distinct values and then order So it wont be the solution .

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