按子集合的子属性对LINQ查询进行排序? [英] Sort LINQ query by child collection's child property?

查看:70
本文介绍了按子集合的子属性对LINQ查询进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些对象:

public class Class
{
     public Guid Id {get;set;}
     public string Name {get;set;}
     public virtual ICollection<Schedule> Schedules{get;set;}
}

public class Schedule
{
     public Guid Id {get;set;}
     public virtual DayOfTheWeekId {get;set;}
     public virtual DayOfTheWeek {get;set;}
     public DateTime StartTime {get;set;}
     public DateTime EndTime {get;set;}
}

我当前的查询看起来像这样,但是我遇到了一个例外:至少一个对象必须实现IComparable.:

My current query looks like this, but I get this exception: At least one object must implement IComparable.:

    Repository
.Get(c => c.Schedules.Any(s => s.DayOfTheWeekTypeId == dayOfTheWeekId))
.OrderBy(e => e.Schedules.OrderBy(s => s.StartDateTime)).ToList()

设置时间时,我总是使用同一天,因为我需要在一周的某些天显示课程.那就是DayOfTheWeek对象起作用的地方.这是我设定时间的方式:

when I set the times i always use the same day, because I need to show classes on certain days of the week. That is where the DayOfTheWeek object comes into play. This is how I am setting the times:

var schedule = new Schedule{
                           StartDateTime = new DateTime(1999,1,1) + new TimeSpan(9, 15, 0),
                           EndDateTime = new DateTime(1999,1,1) + new TimeSpan(9, 15, 0),
                           DayOfTheWeekTypeId = 1
                           }

更新:

考虑到这一点,我想我可能想分组...

Thinking about this, I guess I may want grouping...

推荐答案

您正尝试按序列订购.那甚至是什么意思?

You're trying to order by a sequence. What does that even mean?

按时间表说最早的事件进行订购会更有意义:

It would make more sense to order by - say - the earliest event in the schedule:

.OrderBy(e => e.Schedules.Min(s => s.StartDateTime))

我不知道这是否会奏效,但至少更有意义.(它将在LINQ to Objects中工作.但是我对EF还是一无所知.)

I don't know whether that will work, but it at least makes more sense. (It would work in LINQ to Objects. I have no idea about EF though.)

这篇关于按子集合的子属性对LINQ查询进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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