Get max value by entity framework

Posted by Ray.Chayan under VB.NET on 2/12/2013 | Points: 10 | Views : 8214 | Status : [Member] | Replies : 2
Hi I have table T1 where i have a column C1.
In Column C1 value is varchar type and the value stored is numeric value , for example 1,2,5,7,10 etc.

I want to fetch the max value from this table in respect to integer by entity framework

for examle here max value should be 10

Please send me the code

Thanks in advance




Responses

Posted by: aswinialuri-19361 on: 2/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hiiii


Try this
int maxAge = context.Persons.Max(p => p.Age);


Thanks & Regards
Aswini Aluri

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Ray.Chayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 2/14/2013 [Member] Starter | Points: 25

Up
0
Down
public int getMaxValstring strColName, string strTblName)
{
using (var dbContext = new Entity())
{
string sql = string.Format("select isnull(MAX([{0}]),0) AS [{0}] FROM {1}", strColName, strTblName);

int max = dbContext.ExecuteStoreQuery<int>(sql).SingleOrDefault();
return max;
}
}

just call this method by passing table name and column name for which u want max value

Ray.Chayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response