Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 最后使用空值排序_C#_.net_Linq - Fatal编程技术网

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,这可能也是一个有趣的选择。