LINQ根据对象行中的整数获取10个对象

chr*_*vik 1 c# linq asp.net

我正在尝试从数据库中选择10个用户,按字段"points"及其值排序.点数最多的用户可以在数据库中的任何位置,因此我必须检查所有点行,并选择具有最高"点"整数的行.字段的类型是可空的int.

我试过了 :

var top = (from UserInformation in dbConn.UserInformations 
           orderby UserInformation.Points.Count() descending
           select UserInformation).Take(10);
Run Code Online (Sandbox Code Playgroud)

但我不能这样做,因为.Count()无效int? (nullable),所以我很难使用,.getValueOrDefault()但它也不会让我使用那个.所以我现在有点卡住了.

dee*_*see 5

只需删除Count()as Points不是一个集合,你可以通过a订购Nullable<int>.

var top = (from UserInformation in dbConn.UserInformations 
           orderby UserInformation.Points descending
           select UserInformation).Take(10);
Run Code Online (Sandbox Code Playgroud)