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; }
}
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 }
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 }
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.
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.
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.
Is this a case where I would use SelectMany() or something similar? I'm kind of lost here.