Example 1
List<Country> CountryList = new List<Country>
() {
new Country { ID = 1, Name="United Kingdom", Month="JAN", Income=120},
new Country { ID = 2, Name="United Kingdom", Month="JAN", Income=125},
new Country { ID = 3, Name="United Kingdom", Month="JAN", Income=115},
new Country { ID = 4, Name="United Kingdom", Month="FEB", Income=125},
new Country { ID = 5, Name="United Kingdom", Month="MAR", Income=128},
new Country { ID = 1, Name="USA", Month="JAN",
Income=110},
new Country { ID = 2, Name="USA", Month="FEB",
Income=160},
new Country { ID = 3, Name="USA", Month="MAR",
Income=160},
new Country { ID = 1, Name="Canada", Month="JAN",
Income=100},
new Country { ID = 2, Name="Canada", Month="FEB",
Income=90},
new Country { ID = 3, Name="Canada", Month="MAR",
Income=80},
new Country { ID = 1, Name="Australia", Month="JAN", Income=105},
new Country { ID = 2, Name="Australia", Month="FEB", Income=95},
new Country { ID = 3, Name="Australia", Month="MAR", Income=85}
};
int runningTotal = 0;
var a = CountryList.GroupBy(i
=> new { i.Name, i.Month })
.Select(g
=> new
{
CountryName
= g.Key.Name,
Month
= g.Key.Month,
Count
= g.Count(),
Total
= g.Sum(i => i.Income),
RunningTotal
= runningTotal += g.Sum(i => i.Income),
Average
= g.Average(i => i.Income)
}).ToList();
Example 2
List<int> list = new List<int>()
{ 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 69, 2007 };
int running_total = 0;
var result_set =
from x in list
select new
{
num = x,
running_total = (running_total
= running_total + x)
};
foreach (var v in result_set)
{
Console.WriteLine("list element: {0},
total so far: {1}",
v.num,
v.running_total);
}
Console.ReadLine();
No comments:
Post a Comment