我已经包含了一些示例代码,它正在做你正在做的事情。它从一些示例代码改编来使用两个类而不是静态字符串数组。
结果将生成带有ProductCategory字段的IEnumerable,Products字段包含基于类别的产品分组,最后一列是分组中的计数。
从你问这是我认为你想要的。
class Product
{
public Product(string cat, string name)
{
Category = cat;
ProductName = name;
}
public string Category { get; set;}
public string ProductName { get;set;}
}
class ProductClass
{
public ProductClass (string type)
{
ProductType = type;
}
public string ProductType { get;set;}
}
ProductClass[] productClasses = new ProductClass[]{
new ProductClass("Beverages"),
new ProductClass("Condiments"),
new ProductClass("Vegetables"),
new ProductClass("Dairy Products"),
new ProductClass("Seafood") };
List<Product> products = new List<Product>();
products.Add(new Product("Seafood", "crab sticks"));
products.Add(new Product("Seafood", "lobster"));
products.Add(new Product("Vegetables", "cucumber"));
products.Add(new Product("Seafood", "oysters"));
products.Add(new Product("Condiments", "pepper"));
products.Add(new Product("Condiments", "salt"));
var query =
from pc in productClasses
join product in products on pc.ProductType equals product.Category into gj
select new { ProductCategory= pc.ProductType, Products = gj, Count = gj.Count() };
您正在寻找的是GroupJoin。考虑这[后](http://stackoverflow.com/a/15599143/1039635) – 2014-10-29 08:19:39
嗨,如果你发现答案有帮助也许接受它作为答案? – 2014-10-30 10:43:06