我该如何针对“孙子孙女"对我的活动进行分类?日期 [英] How do i sort my events with regards to "grandchildren's" date

查看:80
本文介绍了我该如何针对“孙子孙女"对我的活动进行分类?日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个EF6 MVC3代码第一站点.我们的事件可能包含 EventRallies 的集合.而且EventRallies可能具有 EventOccasions

We’ve got an EF6 MVC3 codeFirst-site. Our Events may have a collection of EventRallies. And the EventRallies may have a collection of EventOccasions

我想对它们进行排序,以使任何将来发生的事件都排在最前面,然后是所有过去的事件,然后是没有任何集会或特殊场合发生的事件.

I want to order them so that events with any future occasions is at the top, followed by events where all occasions are in the past, and then events that doesn´t have any rallies or occasinons tied to them.

以下尝试会导致:System.ArgumentException:DbSortClause表达式的类型必须具有可比的顺序-error;)

The following attempt results in an :System.ArgumentException: DbSortClause expressions must have a type that is order comparable- error ;)

return context.Events.Where(x => x.OrganizationId == id).
Include(x => x.EventRallies).
Include(x => x.EventRallies.Select(e => e.EventOccasions)).
OrderBy(x => x.EventRallies.OrderByDescending(d=>d.EventOccasions.FirstOrDefault().Date)).ToList();

有什么建议吗?

推荐答案

您需要 SelectMany 来获取一个事件的所有 EventOccasion 的一个列表.选择他们的日期.该列表可以按日期排序(降序).此列表中的第一个日期是事件的订购键.

You need SelectMany to get one list of all EventOccasions of one Event. Select their dates. This list can be ordered by date (descending). The first date in this list is the ordering key for the Events.

如果您按此日期键降序排列,则将来的事件将排在第一位,然后是过去的事件,然后是没有任何场合的事件.

If you order by this date key descending, the future events will come first, then the past events, followed by the events without any occasion.

context.Events.Where(x => x.OrganizationId == id)
.Include(x => x.EventRallies)
.Include(x => x.EventRallies.Select(e => e.EventOccasions))
.OrderByDescending(x => x.EventRallies.SelectMany(d => d.EventOccasions)
    .Select(occ => occ.Date)
    .OrderByDescending(d => d)
    .FirstOrDefault()).ToList();

这篇关于我该如何针对“孙子孙女"对我的活动进行分类?日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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