Friday, July 5, 2013

Fetch maximum of an integer/ numeric/ decimal field of your table using Entity Framework

Whenever there is a need to fetch the maximum of integer/ numeric/ decimal field, you can execute following simple statement to get the same, if your project is using Entity Framework.

Following example is for finding maximum value in an Integer field. Similarly, you can use it for decimal/ double/ etc fields.

Also, consider "int?" (nullable) in below statement, because if there is no record in the particular table, then the below statement will return null. If you use "int" instead of "int?", then it will throw an exception if the table has no record.

int? intIdt = db.Users.Max(u => (int?)u.UserId);

No comments:

Post a Comment

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman