【问题标题】:LINQ group by date descending, and order by time ascendingLINQ 按日期降序分组,按时间升序排序
【发布时间】:2012-01-06 12:34:53
【问题描述】:

我有以下型号:

public class A
{
  public int? Id {get;set;}
  public DateTime Start {get;set;}
  public DateTime End {get;set;}
}

现在我想按日期对所有条目进行分组,并按日期降序对它们进行排序,并按时间升序排序(使用 LINQ)。示例输出:

Jan 3. 2012
-- 10:00-11:00
-- 11:00-13:00
Jan 2. 2012
-- 10:00-11:00
-- 12:00-15:00
Jan 1. 2012
-- 9:00-10:00
-- 12:00-13:00

是否可以在 LINQ-to-SQL 中实现这一点,还是在查询执行后(在结果列表上)我必须这样做?

【问题讨论】:

  • StartEnd 只是 DateTime 条目,例如可视化时间表。

标签: c# linq entity-framework linq-to-sql


【解决方案1】:

你的意思是这样的?

IEnumerable<A> sorted = listOfA.OrderByDescending(a => a.Start.Date)
                               .ThenBy(a => a.Start.TimeOfDay);

【讨论】:

  • 谢谢,但出现以下错误:The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 所以我在查询执行后放置了您的代码,它工作正常 :)
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多