我有一个数据库表。
首先,我想按日期时间分组。
然后,我只想选择包含n个项目的组。
我的课看起来像这样:
public class VisitDate
{
public int Id {get;set;}
public int VisitMeDate {get;set;}
.....
.....
}
我的SQL表如下所示:
Id Date
--- -----------------------
136 2012-05-09 10:00:00.000
167 2012-05-09 12:00:00.000
137 2012-05-10 10:00:00.000
168 2012-05-10 12:00:00.000
194 2012-05-10 14:00:00.000
138 2012-05-11 10:00:00.000
169 2012-05-11 12:00:00.000
195 2012-05-11 14:00:00.000
139 2012-05-12 10:00:00.000
170 2012-05-12 12:00:00.000
196 2012-05-12 14:00:00.000
140 2012-05-13 10:00:00.000
171 2012-05-13 12:00:00.000
197 2012-05-13 14:00:00.000
141 2012-05-14 10:00:00.000
142 2012-05-15 10:00:00.000
172 2012-05-15 12:00:00.000
143 2012-05-16 10:00:00.000
173 2012-05-16 12:00:00.000
144 2012-05-17 10:00:00.000
174 2012-05-17 12:00:00.000
198 2012-05-17 14:00:00.000
我想像下面这样转换:
List<List<MyEntity>> = ?;
137 2012-05-10 10:00:00.000
168 2012-05-10 12:00:00.000
194 2012-05-10 14:00:00.000
138 2012-05-11 10:00:00.000
169 2012-05-11 12:00:00.000
195 2012-05-11 14:00:00.000
139 2012-05-12 10:00:00.000
170 2012-05-12 12:00:00.000
196 2012-05-12 14:00:00.000
140 2012-05-13 10:00:00.000
171 2012-05-13 12:00:00.000
197 2012-05-13 14:00:00.000
144 2012-05-17 10:00:00.000
174 2012-05-17 12:00:00.000
198 2012-05-17 14:00:00.000
我尝试了这个:
List<List<VisitDate>> dates = dbContext.VisitDates.GroupBy(f => new { f.VisitMeDate }).ToList();
但是它不能编译,我也不知道怎么说我只希望包含三个元素的组。
最佳答案
尝试这样的事情:
var result = dbContext.VisitDates
.GroupBy(x => x.VisitMeDate.Date)
.Where(g => g.Count() == 3)
.Select(g => g.ToList())
.ToList();
关于c# - 将实体集合分为n个部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10523718/