C# 最后使用空值排序
我有以下资料:C# 最后使用空值排序,c#,.net,linq,C#,.net,Linq,我有以下资料: public IEnumerable<Customer> SortByRegion(IEnumerable<Customer> customerList) { return customerList.OrderBy(x => x.Region).ThenBy(x => x.ContactName); } customerList.OrderBy(x => x.Region != null).ThenBy(x => x.Co
public IEnumerable<Customer> SortByRegion(IEnumerable<Customer> customerList)
{
return customerList.OrderBy(x => x.Region).ThenBy(x => x.ContactName);
}
customerList.OrderBy(x => x.Region != null).ThenBy(x => x.ContactName);
但是我没有工作。谷歌搜索了一段时间。似乎找不到它。您可以这样做:
OrderBy(x => x.Region != null ? 1 : 0)
好吧,既然
false
对于null
最后一次可以
OrderBy(x => x.Region == null)
你试过在
x.Region
上使用OrderByDescending
吗?这个怎么样:OrderBy(x=>x.Region!=null?1:0)
Salah的答案有效,但你仍然需要按区域排序OrderBy(x=>x.Region==null?1:0)。然后by(x=>x.Region)。然后by(x=>x.ContactName)
。可能是您正在查找customerList.OrderBy(x=>x.Region??string.empty)。然后by(x=>x.ContactName)代码>您好,如果您觉得某个答案帮助您解决了问题,请不要忘记单击答案旁边的灰色复选标记将其标记为已接受。也许OrderBy(x=>x.Region??string.empty)
可能是更好的选择。@MKR Right,这可能也是一个有趣的选择。