Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable .Now I want to filter that datatable using linq .datatable have many column but like
station   max_temp  min_temp   date
XYZ       14.5       3.5        26-5-2016
XYX       13.5       3.5        25-5-2016
ABC       14.5       3.5        26-5-2016
ABC       12.5       5.5       23.5.2016


Now I want to select in datatable like this

XYZ     14.5       3.5        26-5-2016
ABC     14.5       3.6       26.5.2016


What I have tried:

C#
DataView dv = new DataView(dtExtraRowOfXML);
            dv.Sort = "DateTime DESC";

            var x = (from r in dtExtraRowOfXML.AsEnumerable() select r["Station"]).Distinct();


            DataTable dtfinalXML = dt1.AsEnumerable().Union(dtExtraRowOfXML.AsEnumerable(), DataRowComparer.Default).CopyToDataTable();
Posted
Updated 27-May-16 1:03am
v4
Comments
OriginalGriff 27-May-16 2:09am    
And?
What did it do that you didn't expect, or not do that you did?
Remember that we can't see your screen, access your HDD, or read your mind - and we can't run your code because we don't have your data!
Use the "Improve question" widget to edit your question and provide better information.
kumari567 27-May-16 2:20am    
I dont know how to improve but as I make you understand that I have a datatable in which many column in which i have to apply filteration so uniqe station name and latest datewise....that is all
Philippe Mori 27-May-16 9:25am    
Then why you don't say that in your question? You only show us an example and your example is a bad one since you have same date 3 times, your date formatting is not consistant and one station name seems to be misspelled.

You should make far more effort to write correct question if you ever want useful answer.
kumari567 31-May-16 7:53am    
sure sir ....I will try.

1 solution

Something like this should work:
C#
DataTable dtfinalXML = dt1.AsEnumerable()
    .GroupBy(r => r.Field<string>("station"), (key, rows) => rows.OrderByDescending(r => r.Field<DateTime>("date")).First())
    .CopyToDataTable();
 
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