CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Web;
public class Program {
public static void Main() {
Taiwan viewModel = new Taiwan {
Taipei = new List<Township> {
new Township { Section = "新店區", Price = "10" },
new Township { Section = "新店區", Price = "200" },
new Township { Section = "新店區", Price = "40" },
new Township { Section = "信義區", Price = "101" },
new Township { Section = "信義區", Price = "2001" },
new Township { Section = "信義區", Price = "401" }
}
};
var sumOfTownships = viewModel.Taipei
.GroupBy(x => x.Section)
.Select(x => new { Section = x.Key, Price = x.Sum(y => int.Parse(y.Price)) });
foreach(var township in sumOfTownships) {
Console.WriteLine(township.Section + ":" + township.Price);
}
}
}
public class Taiwan {
public IEnumerable<Township> Taipei { get; set; }
}
public class Township {
public string Section { get; set;}
public string Price { get; set;}
}