Do I need to use a nested group statement to solve this LINQ query?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im running a LINQ query that needs to group some ids together and calculate their average, but then group them again by city and calculate the entire cities average based off of those ids.
Ive tried it multiple ways:
<pre class="prettyprint // Get candidates average duration and charge frequency
var candidateDurationFREQ =
(from t in tempData.Tables[queryTable].AsEnumerable()
join c in candidateSearch.AsEnumerable() on (decimal)t.Field<decimal>("PREM_ID") equals (decimal)c.PREMISE
where (t.Field<int>("TIMESTAMP") == (int)(interval - 3) || t.Field<int>("TIMESTAMP") == (int)(interval - 2) || t.Field<int>("TIMESTAMP") == (int)(interval - 1) || t.Field<int>("TIMESTAMP") == (int)(interval))
group t by t.Field<decimal>("PREM_ID") into gr
select new { gr.Key, AVG_DUR = gr.Average(t => t.Field<decimal>("AVG_CHARGE_DURATION")), CHARGE_FREQ = gr.Average(t => t.Field<decimal>("Rate")) });
// Get average duration and charge frequency BY CITY for candidates
var candidateAvgMetricsByCity =
from c in candidateDurationFREQ
join t in tempData.Tables[queryTable].AsEnumerable() on c.Key equals t.Field<decimal>("PREM_ID")
group t by t.Field<string>("SERV_CITY") into gr
select new { gr.Key, AVG_DUR = gr.Average( c.AVG_DUR ), CHARGE_FREQ = gr.Average( c => c.Field<decimal>("Rate") ) }; [/code]
The first LINQ query groups them by ids and calculates their average.
When I try to join the tables and group them by city, I lose Cs attributes as soon as I declare "group t by ... into gr". I can know longer reference c.AVG_DUR or c.CHARGE_FREQ.
Is there a reason I cant access these when I use the group function?
Am I going to have to use

View the full article
 
Back
Top