Created
December 6, 2013 20:47
-
-
Save thinkAmi/7831823 to your computer and use it in GitHub Desktop.
LINQ to Objectsで、複数Join・複合キーでGroup化・並び替えをして、サマリを出す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LinqSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var items = new List<Item> | |
{ | |
new Item(){ ID = 1, ItemName = "りんご" }, | |
new Item(){ ID = 2, ItemName = "みかん" }, | |
new Item(){ ID = 3, ItemName = "ぶどう" } | |
}; | |
var customers = new List<Customer> | |
{ | |
new Customer(){ ID = 1, CustomerName = "hoge" }, | |
new Customer(){ ID = 2, CustomerName = "fuga" } | |
}; | |
var orders = new List<Order> | |
{ | |
new Order() { No = 1, CustomerID = 1, ItemID = 1, Amount = 1 }, | |
new Order() { No = 2, CustomerID = 1, ItemID = 1, Amount = 2 }, | |
new Order() { No = 3, CustomerID = 1, ItemID = 2, Amount = 1 }, | |
new Order() { No = 4, CustomerID = 2, ItemID = 1, Amount = 1 }, | |
new Order() { No = 5, CustomerID = 1, ItemID = 1, Amount = 3 }, | |
new Order() { No = 6, CustomerID = 2, ItemID = 1, Amount = 2 }, // カンマ付もOK | |
}; | |
// o:Order, c:Customer, i:Item, oc:Order&Customer とする | |
var results = orders.Join(customers, o => o.CustomerID, c => c.ID, (o, c) => new { o, c }) | |
.Join(items, oc => oc.o.ItemID, i => i.ID, (oc, i) => new { oc, i }) | |
.GroupBy(g => new | |
{ | |
// 匿名型では同じIDという名前を持てないので、新しく名前をつける | |
// エラー「匿名型では、同じ名前を持つ複数のプロパティを含むことはできません」 | |
CustomerID = g.oc.c.ID, | |
ItemID = g.i.ID | |
}) | |
.OrderByDescending(d => d.Key.CustomerID) | |
.ThenBy(t => t.Key.ItemID) | |
.Select(s => new | |
{ | |
// CustomerNameとItemNameは、グループ化されていれば全部同じはずなので、最初のを使う | |
CustomerName = s.First().oc.c.CustomerName, | |
ItemName = s.First().i.ItemName, | |
Total = s.Sum(sum => sum.oc.o.Amount) | |
}); | |
foreach (var result in results) | |
{ | |
Console.WriteLine("================"); | |
Console.WriteLine(result.CustomerName); | |
Console.WriteLine("----------------"); | |
Console.WriteLine("品名:" + result.ItemName + "、数量:" + result.Total.ToString()); | |
Console.WriteLine("----------------"); | |
Console.WriteLine(""); | |
} | |
Console.WriteLine("Enterキーで終了"); | |
Console.ReadKey(); | |
} | |
public class Order | |
{ | |
public int No { get; set; } | |
public int CustomerID { get; set; } | |
public int ItemID { get; set; } | |
public int Amount { get; set; } | |
} | |
public class Item | |
{ | |
public int ID { get; set; } | |
public string ItemName { get; set; } | |
} | |
public class Customer | |
{ | |
public int ID { get; set; } | |
public string CustomerName { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment