I am using Entity Framework 6 and want to group some data with GROUP BY
. I have to sort both the groups AND the data within a group. To make things as easy as possible I wrote a minimalistic example.
我正在使用Entity Framework 6,并希望将一些数据分组到group BY中。我必须对组内的组和数据进行排序。为了让事情尽可能简单,我写了一个极简主义的例子。
Let's assume we have some articles with a Created-Field (only Date)
. I want to group all articles that are written on one day. And I want to group the articles itself within a group by the Created-Field
.
假设我们有一些带有create - field (only Date)的文章。我想把一天写的所有文章归类。我想将文章本身按照create - field分组。
This was my approach:
这是我的方法:
var articleGroups = DBContext.Articles
.OrderByDescending(x => x.Created)
.GroupBy(x => x.Created)
.OrderByDescending(x => x.Key)
;
The groups are ordered perfectly but the ordering of the group itself is completely ignored. What am I doing wrong?
组是完全有序的,但是组本身的顺序完全被忽略。我做错了什么?
3 个解决方案
#1
2
Thanks for the responses. It seems that I just found a solution to my own problem ;)
谢谢你的回复。我似乎找到了解决我自己问题的办法;
var articleGroups = DBContext.Articles
.GroupBy(x => x.Created)
.Select(x => new {
Created = x.Key,
Items = x.OrderByDescending(y => y.Created)
})
.OrderByDescending(x => x.Created)
;
#2
1
Try this:
试试这个:
var articleGroups = DBContext.Articles
.GroupBy(x => x.Created,
(x, g) => new{Key=x, Group = g.OrderByDescending(c=>c.Created)})
.OrderByDescending(x => x.Key);
This example use signature of GroupBy with element and result selector to leverage on objects in group.
本例使用GroupBy的签名与元素和结果选择器来利用组中的对象。
#3
0
If I understood correctly:
如果我理解正确的话:
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);
Article Class:
文章类:
class Articles
{
public DateTime DateCreated { get; set; }
public string Smth { get; set; }
}
Where ByDate
and GroupedColl
are your data fields.
ByDate和GroupedColl是您的数据字段。
#1
2
Thanks for the responses. It seems that I just found a solution to my own problem ;)
谢谢你的回复。我似乎找到了解决我自己问题的办法;
var articleGroups = DBContext.Articles
.GroupBy(x => x.Created)
.Select(x => new {
Created = x.Key,
Items = x.OrderByDescending(y => y.Created)
})
.OrderByDescending(x => x.Created)
;
#2
1
Try this:
试试这个:
var articleGroups = DBContext.Articles
.GroupBy(x => x.Created,
(x, g) => new{Key=x, Group = g.OrderByDescending(c=>c.Created)})
.OrderByDescending(x => x.Key);
This example use signature of GroupBy with element and result selector to leverage on objects in group.
本例使用GroupBy的签名与元素和结果选择器来利用组中的对象。
#3
0
If I understood correctly:
如果我理解正确的话:
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);
Article Class:
文章类:
class Articles
{
public DateTime DateCreated { get; set; }
public string Smth { get; set; }
}
Where ByDate
and GroupedColl
are your data fields.
ByDate和GroupedColl是您的数据字段。