What would be the Linq code for this grouping using VB.NET 2008?
I have a population aged from 0 to 119 years.
I wish to group this开发者_开发百科 population from the age of 15 to 49 according to the following hypothesis:
- 15 to 19;
- 20 to 24;
- 25 to 29;
- 30 to 34;
- 35 to 39;
- 40 to 44;
- 45 to 49.
Any age apart these are useless for the calculation I need to perform. Here are some sample data and code.
I have an AnnualPopulation class which represents the demographic population for each year of age.
Public Class AnnualPopulation
Public Id as Integer
Public Year As Integer
Public Gender As String
Public YearsOfAge As Double?() = New Double?(119) { }
End Class
Then, the YearsOfAge array/collection property contains the number of people based on their gender. For instance:
AnnualPopulation.Year = 2009
AnnualPopulation.Gender = "F"c
For age As Integer = 0 To AnnualPopulation.YearsOfAge.Length - 1 Step 1
YearsOfAge(age) = 42356.67F 'Of course, this number varies from a years of age to another!'
Next
I would then like to group such as follows:
Dim groups() As Double = New Double(6) { }
For age As Integer = 15 To 49 Step 1
Dim index As Integer = 0
Select Case age
Case 15 to 19
index = 0
Case 20 To 24
index = 1
Case 25 To 29
index = 2
Case 30 To 34
index = 3
Case 35 To 39
index = 4
Case 40 To 44
index = 5
Case 45 To 49
index = 6
End Select
groups(index) += AnnualPopulation.YearsOfAge(age - 1)
Next
This way, I would have the sum of population for each range of year of age
So, I though of grouping them, and then I have no common key to group them by, no luck! =P In C#, I guess this would do what I need:
double[] groups = new double[7];
Enumerable.Range(15, 34).ToList().ForEach(age => {
groups[(int)(age % 4)] += annualPopulation.YearsOfAge[age - 1].HasValue
? annualPopulation.YearsOfAge[age - 1].Value : 0.0;
});
Which I don't seem to be able to achieve in VB.NET 2008 except if I try to go along with New Action(Of T1, T2)
or else Func(Of Double?, TKey)
. I'm not comfortable at all with these! =( (Underlying question: Why is it so complicated in VB.NET to work with lambdas!?)
My code actually works, I'm only looking for a nicer and perhaps more readable solution, though this is easy to understand what I'm trying to achieve here.
Anyway, anyone has a clue about how to go with this?
Thanks in advance! =)
Use your switch
statement in the group by selector; the keys will be 0 to 6 respectively then. After that, you simply can use the Sum
operator on each group.
In C#, it would look like this (you don't have ranged case
s in C#):
var filtered = data
.Where(item => item.Age > 15 && item.Age <= 49)
.GroupBy(item =>
{
if(item.Age > 15 && item.Age <= 19)
return 0;
else if(item.Age <= 24)
return 1;
else if(item.Age <= 29)
return 2;
else if(item.Age <= 34)
return 3;
else if(item.Age <= 39)
return 4;
else if(item.Age <= 44)
return 5;
else if(item.Age <= 49)
return 6;
});
After that, you can create a dictionary:
var dict = filtered.ToDictionary(i => i.Key, i => /* do something with the sequence */);
精彩评论