Ale*_*idt 5 .net c# entity-framework group-by
我正在使用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)
;
Run Code Online (Sandbox Code Playgroud)
这些组被完美地排序,但组本身的排序完全被忽略.我究竟做错了什么?
如果我理解正确的话:
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);
Run Code Online (Sandbox Code Playgroud)
文章类别:
class Articles
{
public DateTime DateCreated { get; set; }
public string Smth { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
哪里ByDate
和GroupedColl
是您的数据字段。
归档时间: |
|
查看次数: |
1846 次 |
最近记录: |