LINQで結合
Joinを使って2つのListを結合
Company.cs
class Company
{
public string Name { get; set; }
public string Market { get; set; }
public Company(string name, string market)
{
Name = name;
Market = market;
}
}
Stock.cs
class Stock
{
public string Name { get; set; }
public int Price { get; set; }
public Stock(string name, int price)
{
Name = name;
Price = price;
}
}
List<Company> companys = new List<Company>();
companys.Add(new Company("あいうえ株式会社", "東証1部"));
companys.Add(new Company("株式会社かきくけ", "東証2部"));
List<Stock> stocks = new List<Stock>();
stocks.Add(new Stock("あいうえ株式会社", 1055));
stocks.Add(new Stock("株式会社かきくけ", 7832));
foreach(var item in companys.Join(stocks, c => c.Name, c => c.Name, (p1, p2) => new { p1.Name, p1.Market, p2.Price}))
{
Debug.WriteLine($"{item.Name} {item.Market} {item.Price} 円");
}
Company.cs
class Company
{
public string Name { get; set; }
public string Market { get; set; }
public Company(string name, string market)
{
Name = name;
Market = market;
}
}
Stock.cs
class Stock
{
public string Name { get; set; }
public int Price { get; set; }
public Stock(string name, int price)
{
Name = name;
Price = price;
}
}
List<Company> companys = new List<Company>();
companys.Add(new Company("あいうえ株式会社", "東証1部"));
companys.Add(new Company("株式会社かきくけ", "東証2部"));
List<Stock> stocks = new List<Stock>();
stocks.Add(new Stock("あいうえ株式会社", 1055));
stocks.Add(new Stock("株式会社かきくけ", 7832));
foreach(var item in companys.Join(stocks, c => c.Name, c => c.Name, (p1, p2) => new { p1.Name, p1.Market, p2.Price}))
{
Debug.WriteLine($"{item.Name} {item.Market} {item.Price} 円");
}
Author And Source
この問題について(LINQで結合), 我々は、より多くの情報をここで見つけました https://qiita.com/lusf/items/c601435f5f228ae24018著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .