I have a list that looks like this.
-100 Smith, Jane $1000
-100 Smith, John $1100.00
-100 Smith, Cole $840.00
-110 Jones, Harry $1270.00
-110 Jones, Diane $870.00
-111 Jones, Richard $1560.00
Using LINQ, I wish to write a query that will allow me to SUM up the values by family. The first identifier is the family id. For the above set of records, my output will look like:
-100 Smith $2940.00
-110 Jones $2140.00
-111 Jones $1560.00
Here's my code:
static void Main(string[] args)
{
_fi = FillIncome();
var query = (from f in _fi group f by f.familyId into delta select new {familyId = delta.Key, amount = delta.Sum(x => x.income) });
foreach (var q in query)
{
Console.WriteLine(q);
}}
static List<FamilyIncome> FillIncome()
{
var income = new List<FamilyIncome>();
var fi = new FamilyIncome {familyId = -100, lastname = "Smith", firstname = "Jane", income = 1000};
income.Add(fi);
fi = new FamilyIncome { familyId = -100, lastname = "Smith", firstname = "John", income = 1100 };
income.Add(fi);
fi = new FamilyIncome { familyId = -100, lastname = "Smith", firstname = "Cole", income = 840 };
income.Add(fi);
fi = new FamilyIncome { familyId = -110, lastname = "Jones", firstname = "Harry", income = 1270 };
income.Add(fi);
fi = new FamilyIncome { familyId = -110, lastname = "Jones", firstname = "Diane", income = 970 };
income.Add(fi);
fi = new FamilyIncome { familyId = -111, lastname = "Jones", firstname = "Richard", income = 1600 };
income.Add(fi);
return income;
}