[英]How to convert IEnumerable<CustomType> to List, applying grouping logic, and convert back to IEnumerable<CustomType>?
I want to take a list of items from a table, apply grouping logic(grouping by assignment type, start and end time), and then convert back to starting type. 我想从表中获取项目列表,应用分组逻辑(按分配类型,开始时间和结束时间分组),然后转换回起始类型。
IEnumerable<Assignment> assignments = UnitOfWork.AssignmentRepository.Get(filter: a => a.Start.CompareTo(start) >= 0
&& a.End.CompareTo(end) <= 0,
orderBy: o => o.OrderBy(a => a.Start));
List<Assignment> pre_alerts = new List<Assignment>();
foreach (Assignment a in assignments)
{
foreach (AssignmentType t in assignmentTypes)
{ // determine if the assignment's type has a minimum required
if (a.AssignmentTypeID == t.AssignmentTypeID && t.Minimum>0)
{
// get the minimum assignments requierd
int min_required = t.Minimum;
DateTime s = a.Start;
DateTime e = a.End;
AssignmentType type = a.AssignmentType;
IEnumerable<Assignment> minimum_assignments = UnitOfWork.AssignmentRepository.Get(filter: m => m.Start.CompareTo(s) >= 0 && m.End.CompareTo(e) <= 0 && m.AssignmentTypeID == a.AssignmentTypeID, orderBy: o => o.OrderBy(m => m.Start));
int min_actual = minimum_assignments.Count();
if (min_actual < min_required)
{
// add a single alert for time range and assignment type
pre_alerts.Add(a);
}
}
}
}
var alerts = pre_alerts.GroupBy(x => new Assignment{ x.AssignmentTypeID, x.Start, x.End }).Select(k => k);
how do I convert alerts to IEnumerable<Assignment>
? 如何将警报转换为IEnumerable<Assignment>
?
When using Select
you are already returning it as an IEnumerable. 使用Select
您已经将其作为IEnumerable返回。 If you want it to be of your type then: 如果您希望它是您的类型,则:
var alerts = pre_alerts.GroupBy(x => new Assignment{ x.AssignmentTypeID, x.Start, x.End }).Select(k => k.Key);
The result of a GroupBy
is an IGrouping
so by Selecting just k
you are returning an IEnumerable<IGrouping<Assignment...>>
GroupBy
的结果是一个IGrouping
因此仅选择k
即可返回IEnumerable<IGrouping<Assignment...>>
But it feels to me that if that is the case then there is no reason to use the GroupBy
but just Select
to create the assignments and then distinct 但是在我看来,如果是这种情况,那么没有理由使用GroupBy
而只需Select
来创建分配,然后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.