1
 ProductCategory = "A"
 Public Class ProdCat
 {
     public   int productId,
     public string  productCategory
}

我有一个这样的清单

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 是一个列表,它有两个属性,即 Product Category 是一个字符串,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>();

}

我认为这现在很清楚了

4

4 回答 4

3

您可以像这样初始化ProductCatGroup

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

您可以使用如下方式ProductCatGroup从示例列表中生成s 列表:GroupBy

        IEnumerable<ProductCatGroup> groups = ProdCatList
            .GroupBy(item => item.productCategory)
            .Select(g => new ProductCatGroup
            {
                ProductCategory = g.Key,
                ProductID = g.Select(x => x.productId).ToList()
            });
于 2012-09-11T05:56:30.433 回答
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
                                                     }
                                  });
于 2012-09-11T07:10:45.763 回答
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() }; 
}

谢谢。

于 2012-09-11T06:10:54.043 回答
0
var grouped = ProductCatList.GroupBy(g => g.ProductCategory )
                 .Select(g => new ProductCatGroup()
                              {
                                  ProductCategory = g.Key,
                                  ProductList = g.Select(p =>  p.productId)
                                                 .ToList()
                              });
于 2012-09-11T07:11:40.317 回答