OrderByDescending不是在LINQ的正常工作 [英] OrderByDescending not working properly in Linq

查看:628
本文介绍了OrderByDescending不是在LINQ的正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ和在C#下面的代码:

I am using Linq and have the following code in C#:

leaves = GetAnnualLeaves();
otherleaves = GetOtherLeaves();
leaves.AddRange(otherleaves);
leaves.OrderByDescending(e => e.StartDate);



基本上我想显示年度和其他假期的名单,我想按降序排列显示它们。问题是,它总是显示其他叶在年节末不管。所以基本上它显示是这样的:

Basically I am trying to display list of annual and other leaves and I want to display them in DESCENDING order. Problem is it always shows "Other Leaves" at the end of "Annual Leaves" no matter what. So basically it displays like this:

2013-06-29 Annual
2012-01-01 Annual
2011-05-02 Annual
2013-05-01 Other

在实际上它应该显示是这样的:

When in fact it should display like this:

2013-06-29 Annual
2013-05-01 Other
2012-01-01 Annual
2011-05-02 Annual

任何想法,为什么由降序排列不工作?

Any idea why order by descending is not working?

修改

我收到的时候我用铸造错误CONCAT。

I am getting casting error when I use CONCAT.

这两个年和其他叶子是同类型的,但是当我做

Both "Annual" and "Other" leaves are of same type but when I do

leaves = leaves.Concat(otherleaves) 

然后该项目不编译并给出错误

then the project doesn't compile and gives error

Cannot implicitly convert type System.Collections.Generic.IEnumerable<MyEntity> to System.Collections.Generic.List<MyEntity>



如果我去做类型转换:

And if I do type cast:

leaves = (List<MyEntity>)leaves.Concat(otherleaves)

然后我得到运行时错误。

then I get runtime error.

推荐答案

您不分配结果 OrderByDescending

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



还是

or do

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

这篇关于OrderByDescending不是在LINQ的正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆