110,538
社区成员
发帖
与我相关
我的任务
分享
public class testLambdaMax
{
public int Id { get; set; }
public string Name { get; set; }
}
testLambdaMax[] lmax= new testLambdaMax[]{
new testLambdaMax(){Id=1,Name="A"},
new testLambdaMax(){Id=2,Name="B"},
new testLambdaMax(){Id=3,Name="C"},
new testLambdaMax(){Id=4,Name="D"}
};
lmax.OrderByDescending(t => t.Id * (t.Name == "B" ? 1 : 0)).First();
lmax.Where(t => t.Name == "B").OrderByDescending(t => t.Id).First();
这里别用用Max方法,因为Max方法返回的只是ID值,还要多一次筛选,效率没有排序的高。