Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compare the date time elements of rows of a dataset and my requirement something like :

i want to fetch the all the dates for all i number of rows returned by the query and store in an array or list or something(what ever you suggest?). Then compare these dates and find the ItemArray which is the earliest/newest of all the elements.

For Instance , below gives me a Data Set row count of 5
C#
ds.Tables[0].Rows[i]ItemArray["DateTime"].ToString()).ToString("dd/MM/yyyy");


and i get following dates
05/07/2012
23/06/2011
13/11/2013
24/04/2014
30/01/2014

Once I fetch the dates from the dataset , I want to compare these and find which is the earliest one.

Here I should get 24/04/2014 as my result as it is the newest.
Posted
Updated 24-May-14 0:16am
v3

C#
var minDate =ds.Tables[0].AsEnumerable().Select(record => record.Field<datetime>("DateTime")).Max();
 
Share this answer
 
v3
First things first: that isn't your code, as that code (a) won't compile and (b) is pretty pointless even if you fix the compilation errors - what do you expect ToString with a date related format string to do when given a string to play with? Because format it as a DateTime is not in the list of things it will do...

DataRow values and DataRowCollections are not directly usable by the sort facilities in C# - you really need to extract the data into a class (and a collection of that class) that can sort easily, and keep the data in it's native format for that - in this case that means DateTime values, not strings as they are much much easier to sort.

So look at your code, work out what data you are feeding into the DataTable and consider creating a class that holds that information. Then load a collection of that from the DataTable, and sort the collection.
 
Share this answer
 
Comments
Kunal Ved 24-May-14 7:04am    
Intitially the statement was
string TestDate = Convert.ToDateTime(ds.Tables[0].Rows[0].ItemArray[5].ToString()).ToString("dd/MM/yyyy")

as it would work for only one row and get me a date converted to a string

But now the thing is I am getting multiple datarows as result in dataset , off all the rows I only want that partucular row where the ItemArray["date"] is the earliest date... Please tell me How i can do that.

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