Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to select the Max value in the data table column and also including where statement in c#.

Thanks,
Sivasankaran G
Posted
Updated 21-Apr-14 0:15am
v2

C#
DateTime maxDate = dt.AsEnumerable()
            .Where(r => r.Field<string>("Column1") == "someValue")
            .Select(r => r.Field<string>("column2"))
            .Max();


http://msdn.microsoft.com/en-us/library/bb386977.aspx[^]
 
Share this answer
 
v2
ypu can use LINQ for this purpose, but i think this is fastest way as compare to LINQ
C#
int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}
 
Share this answer
 
Comments
Gssankar 21-Apr-14 6:22am    
Thanks for your reply. But how to use where statement.
 
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