The code posted by
[)ia6l0 will work only if there are some items in the list.
If there are no items in the list then you will get exception
Take this example
List<Employee> employees = new List<Employee>();
int maxValue = employees.Max(e => e.ID);
The above will return a exception as "Sequence contains no Elements" , As there are no elements in the array the system is not able to find the max value in the collection.
To avoid this error use the DefaultIfEmpty Method id addition
See the below example
List<Employee> employees = new List<Employee>();
int maxValue = employees.Select(emp => emp.ID).DefaultIfEmpty().Max();
employees.Add(new Employee() { ID = 10, Name = "John Hook" });
employees.Add(new Employee() { ID = 12, Name = "Chritopher Null" });
employees.Add(new Employee() { ID = 11, Name = "James Walter Jr." });
maxValue = employees.Select(emp => emp.ID).DefaultIfEmpty().Max();
This works even if the collection contains no elements