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>();
}
我认为这现在很清楚了