Jun 13, 2011

MVC 3 : Select Record Which Has Maximum Value In A Column Using LINQ

Modern programming usually does not use direct query to access data from database.  For those who familiar with C# and VB programming will definitely know about LINQ.  Basically, LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.

For those who core in PHP programming, you might like to read about PHINQ that based on googlecode projects.  I’m not sure which projects does have latest update since most of the project stop around 2009.

So my tutorial will be based on .NET LINQ where I’ll show on how do we select a maximum value from a huge records.  This usually useful when we would want to generate a custom ID instead of depending on identity column.
In native SQL query, the Query should be like this:
var highPost = SELECT MAX(post) as "Highest Number" FROM SQL_TABLE; 

This is an equivalent LINQ statement which will return the same value as the SQL Query above:
var highPost = db.SQL_TABLEs.Select(obj => obj.post).Max(); 

That’s what you need to get the maximum value of a column in SQL table using LINQ.

Please leave a comment and don't forget to like the post if it was helpful.


Blogger Labels: Modern,data,database,LINQ,Framework,language,Visual,Basic,syntax,advantage,PHINQ,tutorial,records,custom,column,Query,SELECT,Highest,Number,FROM,SQL_TABLE,statement,SQL_TABLEs,extensions,operations,highPost

Thank you for your unbelievable support on Negative Zero - Permission to read and write blog for nearly 4 years. Don't forget to like Negative Zero on Facebook.
Blogirific.com Blog Directory

Post(s) you might like to read :



Post a Comment