using System.Collections.Generic;
public static void Main()
List<Customer> customers = new List<Customer>() { new Customer() { Name = "Jan", Surname = "Tichy", OrderedProduct = new Product[] { new Product() { ProductCategory = Product.Category.Food, ProductName = "Neco k jidlu" }, new Product() { ProductCategory = Product.Category.Pets, ProductName = "Neco pro psa" }, new Product() { ProductCategory = Product.Category.Pets, ProductName = "Neco pro rybicky" } } }, new Customer() { Name = "Zuzana", Surname = "Mala", OrderedProduct = new Product[] { new Product() { ProductCategory = Product.Category.Pets, ProductName = "Neco pro kocku" } } } };
var groupProductQuery = from Customer c in customers
from Product p in c.OrderedProduct
group p by p.ProductCategory into ProductGroup
ProductID = ProductGroup.Key,
CustomerList = (from Customer c in customers
from Product p in c.OrderedProduct
where p.ProductCategory == ProductGroup.Key
select new { Surname = c.Surname }).ToList()
Console.WriteLine("Query 5 >>>");
foreach (var gpq in groupProductQuery) {
Console.WriteLine("Category: {0}", gpq.ProductID);
foreach (var cust in gpq.CustomerList) {
Console.WriteLine(" {0}", cust.Surname);
Console.WriteLine("\n---\n");
foreach (var category in Enum.GetNames(typeof(Product.Category)))
Console.WriteLine("Category: " + category);
foreach (var cust in customers.Where(c => c.OrderedProduct.Select(p => p.ProductCategory.ToString()).Contains(category)))
Console.WriteLine("\t" + cust.Surname);
Console.WriteLine("\n---\n");
foreach (var category in Enum.GetValues(typeof(Product.Category)).Cast<Product.Category>())
Console.WriteLine("Category: " + category.ToString());
foreach (var cust in customers.Where(c => c.OrderedProduct.Select(p => p.ProductCategory).Contains(category)))
Console.WriteLine("\t" + cust.Surname);
Console.WriteLine("\n---\n");
var categories = Enum.GetNames(typeof(Product.Category)).Select(n => new { Name = n, Customers = customers.Where(c => c.OrderedProduct.Select(p => p.ProductCategory.ToString()).Contains(n)) });
foreach (var category in categories)
Console.WriteLine("Category: " + category.Name);
foreach (var cust in category.Customers)
Console.WriteLine("\t" + cust.Surname);
Console.WriteLine("\n---\n");
Console.WriteLine(String.Join("\n", Enum.GetValues(typeof(Product.Category)).Cast<Product.Category>().Select(pc => "Category: " + pc.ToString() + "\n" + String.Join("\n", customers.Where(c => c.OrderedProduct.Select(p => p.ProductCategory).Contains(pc)).Select(c => "\t" + c.Surname)))));
public string ProductName { get; set; }
public Category ProductCategory { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Product[] OrderedProduct { get; set; }