Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Queryable Max Method
The Queryable.Max() method in C# returns the maximum value from a sequence of numeric values. It operates on IQueryable<T> collections and provides query optimization for data sources like Entity Framework.
Syntax
Following is the syntax for the Max() method −
public static TSource Max<TSource>(this IQueryable<TSource> source) public static TResult Max<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector)
Parameters
-
source − An
IQueryable<T>sequence of values to determine the maximum value of. -
selector − A transform function to apply to each element (optional overload).
Return Value
Returns the maximum value in the sequence. The return type depends on the source type or the selector function's return type.
Using Max() with Numeric Collections
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<long> numbers = new List<long> { 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000 };
Console.WriteLine("Numbers in the list:");
foreach(long num in numbers) {
Console.WriteLine(num);
}
// Getting the maximum value
long maxNumber = numbers.AsQueryable().Max();
Console.WriteLine("Largest number = {0}", maxNumber);
}
}
The output of the above code is −
Numbers in the list: 200 400 600 800 1000 1200 1400 1600 1800 2000 Largest number = 2000
Using Max() with Selector Function
You can use a selector function to find the maximum value based on a property of objects −
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Student {
public string Name { get; set; }
public int Age { get; set; }
public double Grade { get; set; }
}
class Program {
static void Main() {
List<Student> students = new List<Student> {
new Student { Name = "Alice", Age = 20, Grade = 85.5 },
new Student { Name = "Bob", Age = 22, Grade = 92.3 },
new Student { Name = "Charlie", Age = 19, Grade = 78.9 }
};
// Find maximum age
int maxAge = students.AsQueryable().Max(s => s.Age);
Console.WriteLine("Maximum age: " + maxAge);
// Find maximum grade
double maxGrade = students.AsQueryable().Max(s => s.Grade);
Console.WriteLine("Maximum grade: " + maxGrade);
}
}
The output of the above code is −
Maximum age: 22 Maximum grade: 92.3
Using Max() with Different Data Types
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
// Integer collection
List<int> integers = new List<int> { 5, 15, 3, 25, 8 };
int maxInt = integers.AsQueryable().Max();
Console.WriteLine("Max integer: " + maxInt);
// Double collection
List<double> doubles = new List<double> { 3.14, 2.71, 1.41, 9.81 };
double maxDouble = doubles.AsQueryable().Max();
Console.WriteLine("Max double: " + maxDouble);
// String collection (lexicographic comparison)
List<string> strings = new List<string> { "apple", "zebra", "banana", "orange" };
string maxString = strings.AsQueryable().Max();
Console.WriteLine("Max string: " + maxString);
}
}
The output of the above code is −
Max integer: 25 Max double: 9.81 Max string: zebra
Queryable vs Enumerable Max
| Queryable.Max() | Enumerable.Max() |
|---|---|
Works with IQueryable<T>
|
Works with IEnumerable<T>
|
| Generates expression trees for query providers | Executes immediately in memory |
| Optimized for database queries (Entity Framework) | Better for in-memory collections |
Conclusion
The Queryable.Max() method efficiently finds the maximum value in queryable sequences and supports both direct value comparison and custom selector functions. It's particularly useful when working with database queries through Entity Framework, as it translates to optimized SQL queries rather than loading all data into memory.
