Skip to content

Instantly share code, notes, and snippets.

@zaconeco
Created June 22, 2016 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaconeco/1192877928dfdcee5224c4f0454d9ec8 to your computer and use it in GitHub Desktop.
Save zaconeco/1192877928dfdcee5224c4f0454d9ec8 to your computer and use it in GitHub Desktop.
// アパレルクラス
private class APPAREL
{
public APPAREL()
{
}
public APPAREL(string shop, string itemName, Sizes size, int stock, int price)
{
this.Shop = shop;
this.ItemName = itemName;
this.Size = size;
this.StockCount = stock;
this.Price = price;
}
// 店舗名
public string Shop { get; set; }
// アイテム名
public string ItemName { get; set; }
// サイズ
public Sizes Size { get; set; }
// 在庫数
public int StockCount { get; set; }
// 販売価格
public int Price { get; set; }
// Size
public enum Sizes
{
XXS = 0,
XS,
S,
M,
L,
XL,
XXL
}
///
/// GroupByメソッド
/// 全店舗のアイテム・サイズ毎の在庫数、販売価格を取得するメソッド
///
///
/// Group byするリスト
///
///
/// Group byしたリスト
///
public static List GroupBy(List list)
{
List gbyList = (from a in list
orderby a.ItemName,a.Size
group a by new { a.ItemName, a.Size } into g
select new APPAREL
{
Shop = String.Join(",", g.Select(s => s.Shop)), // ショップ名はカンマ区切りで結合
ItemName = g.Max(s => s.ItemName), // 文字列でもMaxでOK
Size = g.Max(s => s.Size), // 列挙体でもMaxでOK
StockCount = g.Sum(s => s.StockCount),
Price = g.Sum(s => s.Price)
}).ToList();
return gbyList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment