LoginSignup
2
2

More than 5 years have passed since last update.

LINQでグループ分け

Last updated at Posted at 2016-09-20

GroupByを使ってグループ分け

株式市場に応じて、会社をグループ分けする。

Company.cs
class Company
{
    public string Name { get; set; }
    public string Market { get; set; }

    public Company(string name, string market)
    {
        Name = name;
        Market = market;
    }
}
List<Company> companys = new List<Company>();

companys.Add(new Company("あいうえ株式会社", "東証1部"));
companys.Add(new Company("株式会社かきくけ", "東証2部"));
companys.Add(new Company("株式会社さしすせ", "東証1部"));

foreach(var m in companys.GroupBy(c => c.Market))
{
    Debug.WriteLine($"market: {m.Key}");
    foreach(var c in m)
    {
        Debug.WriteLine($"company: {c.Name}");
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2