2012-09-11 23 views
1
ProductCategory = "A" 
Public Class ProdCat 
{ 
    public int productId, 
    public string productCategory 
} 

我有这样使用LINQ构建一个列表C#

List<ProdCat> ProdCatList = new List<ProdCat>(); 
ProdCatList.Add(new ProdCat{ productId= 1, productCategory= "A" }); 
ProdCatList.Add(new ProdCat{ productId= 2, productCategory= "B" }); 
ProdCatList.Add(new ProdCat{ productId= 3, productCategory= "A" }); 
ProdCatList.Add(new ProdCat{ productId= 4, productCategory= "C" }); 
ProdCatList.Add(new ProdCat{ productId= 5, productCategory = "A" }); 
ProdCatList.Add(new ProdCat{ productId = 6, productCategory= "B" }); 

我需要拿出像这样的列表清单。请注意ProductCatList是一个列表,它有两个属性,即产品类别是一种刺痛,productIds这是ProductCatList另一个列表

ProductCatList 
    ProductCategory = "A" 
     productId = 1 
     productId = 3 
     productId = 5 
    ProductCategory = "B" 
     productId = 2 
     productId = 6 
    ProductCategory = "C" 
     productId = 4 

类实现会像下面

Public Class ProductCatGroup 
{ 
    string  ProductCategory , 
    IList <int> ProdcutId 

} 

我怎么能实现上述使用LINQ


像我有两个类,如下

public class product 
      { 

       public int productId { get; set; } 
       public string productName { get; set; } 
      } 

      public class ProductCat 
      { 


       public int productId { get; set; } 
       public string productName { get; set; } 
       public int productCatId { get; set; } 
       public string ProductCatName { get; set; } 
      } 

名单如下

List<ProdCat> ProdCatList = new List<ProdCat>(); 
    ProdCatList.Add(new ProdCat{ productId= 1, productCatId = "1", productName="PA", ProductCatName = "PC1"}); 
    ProdCatList.Add(new ProdCat{ productId= 2, productCatId = "2", productName="PB", ProductCatName = "PC2"}); 
    ProdCatList.Add(new ProdCat{ productId= 3, productCatId = "2", productName="PC", ProductCatName = "PC2"}); 
    ProdCatList.Add(new ProdCat{ productId= 4, productCatId = "1", productName="PD", ProductCatName = "PC1"}); 
    ProdCatList.Add(new ProdCat{ productId= 5, productCatId = "1", productName="PE", ProductCatName = "PC1"}); 

,我需要拿出类似下面

ProductCatGroup 
     productCatId = "1" 
     ProductCatName = "PC1" 
      productId = 1 
      productName = "PA" 
      productId = 4 
      productName = "PD" 
      productId = 5 
      productName = "PE" 
     productCatId = "2" 
     ProductCatName = "PC2" 
      productId = 2 
      productName = "PB" 
      productId = 3 
      productName = "PC" 

类实现如下

public class ProductCatGroup 
{ 
      public int productCatId { get; set; } 
      public int productCatName{ get; set; } 
      public IList<Product> ProductList = new List<Product>(); 

} 

我认为这是现在很清楚

+1

什么与所有的'输入代码here'的? –

+0

'Public'和'Class'需要小写。 – Adam

+1

我甚至不知道你在这里问什么,为什么LINQ会成为一个需求? –

回答

1
var grouped = ProductCatList.GroupBy(g => new{g.productCatId,g.productCatName}) 
        .Select(g => new ProductCatGroup() 
            { 
             productCatId = g.Key.productCatId, 
             productCatName = g.Key.productCatName, 
             ProductList = g.Select(p => new Prroduct() 
                { 
                 productid = p.productId, 
                 productName = p.productName 
                } 
            }); 
+0

这将用于我的目的....非常感谢你...它帮助我编写了一个复杂的foreach循环..感谢所有其他人以及试图帮助我 – Coder

3

您可以初始化ProductCatGroup这样的:

 ProductCatGroup g = new ProductCatGroup 
     { 
      ProductCategory = "A", 
      ProductID = (new int[] { 1, 3, 5 }).ToList() 
     }; 

您可以从您的示例使用列表中GroupBy这样产生的ProductCatGroup个清单:

 IEnumerable<ProductCatGroup> groups = ProdCatList 
      .GroupBy(item => item.productCategory) 
      .Select(g => new ProductCatGroup 
      { 
       ProductCategory = g.Key, 
       ProductID = g.Select(x => x.productId).ToList() 
      }); 
+0

我想拿出一个列表,其中包含ProductCatergory和产品ID列表 – Coder

0

找到下面的代码,如果它可以帮助你

public class ProdCat 
{ 
    public int productId; 
    public string productCategory; 
} 

public class ProductCatGroup 
{ 
    public string ProductCategory; 
    public IList<int> ProdcutId; 

} 
public Form1() 
{ 
    List<ProdCat> ProdCatList = new List<ProdCat>(); 
    ProdCatList.Add(new ProdCat { productId = 1, productCategory = "A" }); 
    ProdCatList.Add(new ProdCat { productId = 2, productCategory = "B" }); 
    ProdCatList.Add(new ProdCat { productId = 3, productCategory = "A" }); 
    ProdCatList.Add(new ProdCat { productId = 4, productCategory = "C" }); 
    ProdCatList.Add(new ProdCat { productId = 5, productCategory = "A" }); 
    ProdCatList.Add(new ProdCat { productId = 6, productCategory = "B" }); 

    var productCatList = from pc in ProdCatList 
     group pc by pc.productCategory into pcGroup 
     select new ProductCatGroup { ProductCategory = pcGroup.Key, ProdcutId = pcGroup.Select(x => x.productId).ToList() }; 
} 

谢谢。

0
var grouped = ProductCatList.GroupBy(g => g.ProductCategory) 
       .Select(g => new ProductCatGroup() 
           { 
            ProductCategory = g.Key, 
            ProductList = g.Select(p => p.productId) 
               .ToList() 
           });