By user1907849


2020-09-16 06:52:34 8 Comments

I have an entity as follows.

SchoolDetails

ID Name Rank  School
1  A           Lawrence
2  B      1    Lawrence
3  C      2    Lawrence

I need to fetch the details of student in a particular school(Lawrence) who has highest rank. In this example ID 2 is the desired output.

My linq query is:

var result = SchoolDetails.Where(x=>x.School == "Lawrence").Max(y=>y.Rank).SingleOrDefault();

I get a compliation message saying that "int? doesnt contain a definition for SingleOrDefault"

I cannot change the datatype of Rank column to make it non nullable since it is a production code. So, given this situation how can I fetch a record with highest rank?

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] C# Equivalent of SQL Server DataTypes

  • 2009-01-08 18:51:08
  • George Stocker
  • 501700 View
  • 614 Score
  • 5 Answer
  • Tags:   c# .net sql-server

10 Answered Questions

[SOLVED] Group by in LINQ

  • 2011-09-06 19:44:20
  • test123
  • 1351645 View
  • 1080 Score
  • 10 Answer
  • Tags:   c# linq group-by

20 Answered Questions

[SOLVED] Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

23 Answered Questions

[SOLVED] LINQ query on a DataTable

14 Answered Questions

[SOLVED] When to use .First and when to use .FirstOrDefault with LINQ?

  • 2009-06-21 19:15:29
  • Metro Smurf
  • 575135 View
  • 839 Score
  • 14 Answer
  • Tags:   c# .net linq

12 Answered Questions

[SOLVED] LINQ Aggregate algorithm explained

  • 2011-08-18 09:51:21
  • Alexander Beletsky
  • 262863 View
  • 735 Score
  • 12 Answer
  • Tags:   c# .net linq aggregate

16 Answered Questions

[SOLVED] Update all objects in a collection using LINQ

  • 2008-12-29 22:15:23
  • lomaxx
  • 534172 View
  • 520 Score
  • 16 Answer
  • Tags:   c# .net linq foreach

14 Answered Questions

[SOLVED] Using LINQ to remove elements from a List<T>

  • 2009-05-12 15:56:24
  • TK.
  • 638229 View
  • 672 Score
  • 14 Answer
  • Tags:   c# .net linq list

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 619345 View
  • 1615 Score
  • 7 Answer
  • Tags:   linq sql-order-by

Sponsored Content