C# 按品牌然后按类别对产品进行分组-linq
有三个表:C# 按品牌然后按类别对产品进行分组-linq,c#,linq,C#,Linq,有三个表: Brand Category ----------------------- -------------------------------- BrandID BrandName CategoryID CategoryName ----------------------- -------------------------------- 1
Brand Category
----------------------- --------------------------------
BrandID BrandName CategoryID CategoryName
----------------------- --------------------------------
1 Brand1 1 cat1
2 Brand2 2 cat2
Product
------------------------------------------------------------
ProductID BrandID CategoryID ProductName
------------------------------------------------------------
1 1 1 Product1
2 1 1 Product2
3 1 2 Product3
4 2 1 Product4
我想编写Llinq查询以获得如下输出:
Brand1
|__ cat1
| |__ Product1
| |__ Product2
|
|__ Cat2
| |__ Product3
|
|__ Brand2
|__ cat1
| |__ Product4
我编写此查询是为了按类别获取产品组:
var query2 = db.Brands
.GroupBy(x => new { x.BrandID, x.BrandName })
.Select(x => new
{
x.Key.BrandID,
x.Key.BrandName,
ProductsNames = db.Products
.Where(p => p.BrandID == x.Key.BrandID)
.Select(p => p.ProductName).ToList()
}).ToList();
如何按品牌然后按类别对产品进行分组?您可以通过两个步骤完成此操作
//Step 1 - Join 3 lists
var query = (from p in products
join b in brands on p.BrandId equals b.Id
join c in categories on p.CategoryId equals c.Id
select new
{
b.BrandName,
c.CategoryName,
p.ProductName
}).ToList();
//Step 2 - query required results
var results = query
.GroupBy(x => new { x.BrandName, x.CategoryName })
.Select(x => new
{
Brand = x.Key.BrandName,
Category = x.Key.CategoryName,
Product = x.Select(y=>y.ProductName).ToList()
});
Console.WriteLine(JsonConvert.SerializeObject(results));
输出
[
{
"Brand": "Brand1",
"Category": "cat1",
"Product": [
"Product1",
"Product2"
]
},
{
"Brand": "Brand1",
"Category": "cat2",
"Product": [
"Product3"
]
},
{
"Brand": "Brand2",
"Category": "cat1",
"Product": [
"Product4"
]
}
]
我写了一个小控制台程序:
class Program
{
static void Main(string[] args)
{
var products = new List<Product>{
new Product { ProductID = 1, BrandID = 1, CategoryID = 1, ProductName = "Product1" },
new Product { ProductID = 2, BrandID = 1, CategoryID = 1, ProductName = "Product2" },
new Product { ProductID = 3, BrandID = 1, CategoryID = 2, ProductName = "Product3" },
new Product { ProductID = 4, BrandID = 2, CategoryID = 1, ProductName = "Product4" }
};
var groupedByBrand =
products
.GroupBy(p => p.BrandID)
.Select(g => g.GroupBy(p => p.CategoryID));
foreach (var groupByBrand in groupedByBrand)
foreach (var groupByCategory in groupByBrand)
foreach (var product in groupByCategory)
Console.WriteLine($"{product.BrandID} - {product.ProductName}");
}
}
public class Product
{
public int ProductID { get; internal set; }
public int BrandID { get; internal set; }
public int CategoryID { get; internal set; }
public string ProductName { get; internal set; }
}
我按BrandId分组,然后在这个组中按categoryId分组
如果只是关于分组,您可以使用BrandID和CategoryID,甚至不需要加入表。如果您想按名称排序,那么当然需要加入
也许你想在分组的基础上做一些排序,而不是分组?一步到位,更加紧凑
public class Product
{
public int ProductID { get; internal set; }
public string ProductName { get; internal set; }
public Brand brand { get; set; }
public Category cat { get; set; }
public Product()
{
brand = new Brand();
cat = new Category();
}
}
public class Brand
{
public int BrandID { get; internal set; }
public string BrandName { get; internal set; }
}
public class Category
{
public int CategoryID { get; internal set; }
public string CategoryName { get; internal set; }
}
private void a1()
{
var products = new List<Product>{
new Product { ProductID = 1,
brand =new Brand { BrandID = 1, BrandName = "Brand1" },
cat = new Category { CategoryID = 1, CategoryName = "cat1" },
ProductName = "Product1" },
new Product { ProductID = 2,
brand =new Brand { BrandID = 1, BrandName = "Brand1" },
cat = new Category { CategoryID = 1, CategoryName = "cat1" },
ProductName = "Product2" },
new Product { ProductID = 3,
brand =new Brand { BrandID = 1, BrandName = "Brand1" },
cat = new Category { CategoryID = 2, CategoryName = "cat2" },
ProductName = "Product3" },
new Product { ProductID = 4,
brand =new Brand { BrandID = 2, BrandName = "Brand2" },
cat = new Category { CategoryID = 1, CategoryName = "cat1" },
ProductName = "Product4" }
};
var g1 = products.GroupBy(p => new { p.brand, p.cat, p })
.Select(y => new
{
k1 = y.Key.brand.BrandName,
k2 = y.Key.cat.CategoryName,
k3 = y.Key.p.ProductName
}
);
}
// Result/Output as below
{ k1 = "Brand1", k2 = "cat1", k3 = "Product1" }
{ k1 = "Brand1", k2 = "cat1", k3 = "Product2" }
{ k1 = "Brand1", k2 = "cat2", k3 = "Product3" }
{ k1 = "Brand2", k2 = "cat1", k3 = "Product4" }
您使用的是没有类别的db.Brands表。而是使用表db.Products.Yes,我需要向用户显示品牌和类别的名称。