如何使用LINQ将这些数据组织到所需的对象中? [英] How can I organize this data into the objects I want with LINQ?
问题描述
我有一个下面的类,我正在尝试使用我查询的一堆数据创建一个列表:
I have an the following class I'm trying to create a list of with a bunch of data I have queried:
public class Agency
{
public string AgencyName { get; set; }
public int AgencyID { get; set; }
public IEnumerable<AuditRule> rules { get; set; }
}
其中"AuditRule"为:
Where "AuditRule" is:
public class AuditRule
{
public int AuditRuleID { get; set; }
public string AuditRuleName { get; set }
public int AvgDaysWorked { get; set; }
}
现在,在进行相当复杂的查询之后,我已经能够将数据放入一个看起来像这样的匿名对象中:
Right now, after a fairly complicated query I have been able to get my data into an anonymous object that looks like:
{ DateImported, AgencyID, AgencyName, AuditRuleName, AuditRuleID, DaysWorked }
我有成千上万种采用这种格式的记录,并且我正在尝试构造一个IEnumerable
I have thousands of records in that format, and I'm trying to construct a IEnumerable<Agency
> which will itself contain a list of rules for each agency.
因此,以以下数据为例:
So, take the following data as an example:
{ AgencyID = 1, AgencyName = "CCR", AuditRuleName = "Call", AuditRuleID = 1, DaysWorked = 3 }
{ AgencyID = 1, AgencyName = "CCR", AuditRuleName = "Call", AuditRuleID = 1, DaysWorked = 5 }
{ AgencyID = 1, AgencyName = "CCR", AuditRuleName = "Time", AuditRuleID = 2, DaysWorked = 2 }
{ AgencyID = 2, AgencyName = "YRX", AuditRuleName = "Call", AuditRuleID = 1, DaysWorked = 3 }
{ AgencyID = 2, AgencyName = "YRX", AuditRuleName = "Acct", AuditRuleID = 3, DaysWorked = 2 }
因此,根据这些数据,我试图首先构造一个代理商列表(顶部的类),对于该示例,显然是"CCR"和"YRX".然后,对于每个代理商,我想要在其IEnumerable
So, out of this data, I'm trying to construct first a list of agencies (the class at the top), which would obviously be "CCR" and "YRX" for this example. Then, for each agency I want an entry for each of its rules in its IEnumerable<AuditRule
> that there is a record for.
因此,代理商"CCR"将有2条规则条目,呼叫"和时间". 通话" AuditRule条目的AvgDaysWorked为4,因为我们平均对代理商"CCR"下通话"的条目进行了3天和5天的工作.时间AuditRule条目的AvgDaysWorked为2,因为只有一个条目.
So, Agency "CCR" would have have 2 Rule entries, "Call" and "Time". The "Call" AuditRule entry will have an AvgDaysWorked of 4 since we are averaging the entries for "Call" under Agency "CCR" which is 3 days and 5 days worked. The Time AuditRule entry would have an AvgDaysWorked of 2, since there is only one entry.
具有LINQ忍者技能的人是否可以帮助我将这些数据整理到每个代理商的代理商和规则列表中(该规则/代理商组合的平均工作天数)?
Can someone with some LINQ ninja skills please help me wrangle this data into a list of Agencies and Rules for each agency (with the avg of days worked for that rule/agency combo)?
对于我来说,甚至能够使匿名对象聚在一起也非常复杂,而且我真的很努力编写此查询来创建此代理商/审计规则列表.
It was complicated enough for me to even be able to get together that anonymous object and I'm really struggling writing this query to create this list of Agencies/AuditRules.
是这种情况下,我会使用SelectMany()或类似的东西吗?我在这里迷路了.
Is this a case where I would use SelectMany() or something similar? I'm kind of lost here.
推荐答案
我希望这能起到作用.
// GetAnonymousCollection() is hard to define... ;)
var anonymous = GetAnonymousCollection();
IEnumerable<Agency> result = anonymous
.GroupBy(a => a.AgencyID)
.Select(ag => new Agency()
{
AgencyID = ag.Key,
AgencyName = ag.First().AgencyName,
Rules = ag
.GroupBy(agr => agr.AudiRuleID)
.Select(agrg => new AuditRule()
{
AuditRuleID = agrg.Key,
AuditRuleName = agrg.First().AuditRuleName,
AvgDaysWorked = (Int32)agrg.Average(agrgr => agrgr.DaysWorked)
})
});
顺便说一句,请考虑在平均时间上使用小数或浮点数.
By the way, think about using a decimal or float for the average time.
这篇关于如何使用LINQ将这些数据组织到所需的对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!