【问题标题】:Sort within a group in Entity Framework在实体框架中的组内排序
【发布时间】:2016-02-07 10:38:52
【问题描述】:

我正在使用 Entity Framework 6 并希望将一些数据与 GROUP BY 分组。我必须对组和组内的数据进行排序。为了让事情尽可能简单,我写了一个简约的例子。

假设我们有一些带有Created-Field (only Date) 的文章。我想对一天写的所有文章进行分组。我想通过Created-Field将文章本身分组到一个组中。

这是我的方法:

var articleGroups = DBContext.Articles
    .OrderByDescending(x => x.Created)
    .GroupBy(x => x.Created)
    .OrderByDescending(x => x.Key)
;

组的顺序完美,但组本身的顺序被完全忽略。我做错了什么?

【问题讨论】:

  • 不是说这是解决方案,但是您是否尝试过在内存中而不是在数据库中进行分组和排序? (也就是在GroupBy()前加一个ToList()),行吗?
  • 我已经尝试过了,它可以工作。在内存中排序非常有效,但它不是我的解决方案,因为我的应用程序应该是可扩展的。我的应用程序中的记录数量一点也不小。
  • 顺便说一句,如果这是 Linq to Objects,那么您首先进行排序的初始方法是完全可以接受的:The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source. (Source) 在 Linq to Entity 中,不能保证这种行为,但取决于提供者。
  • 请告诉我您对我的回答的看法
  • 我不在数据库中使用计算列。相反,我使用命名空间 System.Data.Entity 中的 DbFunctions.TruncateTime(DateTime)。 TruncateTime 方法映射到 SQL,因此在数据库上执行。希望对您有所帮助...

标签: c# .net entity-framework group-by


【解决方案1】:

如果我理解正确:

List<Articles> list = new List<Articles>() 
{
   new Articles(){DateCreated=new DateTime(2015, 1, 18), Smth="aa1"},
   new Articles(){DateCreated=new DateTime(2015, 1, 18), Smth="aa2"},
   new Articles(){DateCreated=new DateTime(2014, 1, 18), Smth="aa3"},
   new Articles(){DateCreated=new DateTime(2014, 1, 18), Smth="aa4"},
   new Articles(){DateCreated=new DateTime(2016, 1, 18), Smth="aa5"},
   new Articles(){DateCreated=new DateTime(2016, 1, 18), Smth="aa6"},
   new Articles(){DateCreated=new DateTime(2012, 1, 18), Smth="aa7"},
   new Articles(){DateCreated=new DateTime(2012, 1, 18), Smth="aa8"},
   new Articles(){DateCreated=new DateTime(2018, 1, 18), Smth="aa9"},
   new Articles(){DateCreated=new DateTime(2018, 1, 18), Smth="aa10"},
};
var yourQuery = (from p in list group p.DateCreated by p.DateCreated into g select new { ByDate = g.Key, GroupedColl=g.ToList() }).OrderBy(x=>x.ByDate);

文章分类:

class Articles 
{
    public DateTime DateCreated { get; set; }
    public string Smth { get; set; }
}

ByDateGroupedColl 是您的数据字段。

【讨论】:

  • 在这种情况下什么是“汽车”?您在哪里对组本身进行排序?
【解决方案2】:

感谢您的回复。看来我刚刚找到了解决自己问题的方法;)

var articleGroups = DBContext.Articles
    .GroupBy(x => x.Created)
    .Select(x => new {
        Created = x.Key,
        Items = x.OrderByDescending(y => y.Created)
    })
    .OrderByDescending(x => x.Created)
;

【讨论】:

    【解决方案3】:

    试试这个:

    var articleGroups = DBContext.Articles
        .GroupBy(x => x.Created, 
                 (x, g) => new{Key=x, Group = g.OrderByDescending(c=>c.Created)})
        .OrderByDescending(x => x.Key);
    

    此示例使用带有元素和结果选择器的 GroupBy 签名来利用组中的对象。

    【讨论】:

    • 不幸的是,这不起作用。 'g' 没有方法 OrderByDescending。你测试过你的代码吗?
    • 我更新答案。在没有 IDE 的情况下编写代码并不容易 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多