Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# OrderByDescending在Linq中工作不正常_C#_Linq - Fatal编程技术网

C# OrderByDescending在Linq中工作不正常

C# OrderByDescending在Linq中工作不正常,c#,linq,C#,Linq,我正在使用Linq,在C#中有以下代码: 基本上,我试图显示年假和其他假期的列表,我想按降序显示它们。问题是无论什么,它总是在“年假”的末尾显示“其他叶子”。所以基本上它是这样显示的: 2013-06-29 Annual 2012-01-01 Annual 2011-05-02 Annual 2013-05-01 Other 2013-06-29 Annual 2013-05-01 Other 2012-01-01 Annual 2011-05-02 Annual 实际上,它应该显示如下:

我正在使用Linq,在C#中有以下代码:

基本上,我试图显示年假和其他假期的列表,我想按降序显示它们。问题是无论什么,它总是在“年假”的末尾显示“其他叶子”。所以基本上它是这样显示的:

2013-06-29 Annual
2012-01-01 Annual
2011-05-02 Annual
2013-05-01 Other
2013-06-29 Annual
2013-05-01 Other
2012-01-01 Annual
2011-05-02 Annual
实际上,它应该显示如下:

2013-06-29 Annual
2012-01-01 Annual
2011-05-02 Annual
2013-05-01 Other
2013-06-29 Annual
2013-05-01 Other
2012-01-01 Annual
2011-05-02 Annual
你知道为什么降序不起作用吗

编辑

当我使用CONCAT时,我得到了铸造错误

“一年生”和“其他”叶子都是同一类型的,但当我这样做的时候

leaves = leaves.Concat(otherleaves) 
然后项目不编译并给出错误

Cannot implicitly convert type System.Collections.Generic.IEnumerable<MyEntity> to System.Collections.Generic.List<MyEntity>
无法将类型System.Collections.Generic.IEnumerable隐式转换为System.Collections.Generic.List
如果我输入cast:

leaves = (List<MyEntity>)leaves.Concat(otherleaves)
leaves=(列表)leaves.Concat(其他leaves)
然后我得到运行时错误

OrderByDescending


不对列表排序,它返回一个新的排序的
IEnumerable
。因此,使用
OrderByDescending的结果来显示您的项目。

您不指定
OrderByDescending的结果

leaves = leaves.OrderByDescending(e => e.StartDate).ToList();
或者

leaves = leaves.Concat(otherleaves).OrderByDescending(e => e.StartDate).ToList();

OrderByDescending
不会“就地排序”。它返回一个必须分配给变量的
IOrderedEnumerable

var orderedLeaves = leaves.OrderByDescending(e => e.StartDate);

leaves=leaves.OrderByDescending(e=>e.StartDate)?如果类型不匹配,也可以更改变量。我编写了第一个代码,但仍然存在相同的问题。现在,在年假中,项目已排序,但其他叶子仍在末尾。和
Leaves.AddRange(otherleaves)在此之前被调用?您是否尝试了第二个示例?是的,这是在“按代码订购”之前。让我试试第二个例子。我使用了第二个代码,现在其他的叶子都没有显示:)有一个打字错误。应该是
Concat
,而不是
Concat
,但除此之外,它在此处工作。现在在年假中,项目已排序,但其他叶子仍在末尾。