我有5个变种表,详情如下:
产品表:
ProductID Name
12 T-Shirt
变体表:
VariantID ProductID Name
1 12 Size
2 12 Color
3 12 Material
VariantOption表:
VariantOptionID VariantID VariantOptionName
1 1 Small
2 1 Medium
3 2 Red
4 2 Blue
5 3 Cotton
6 3 Lawn
Sku表:
SkuID ProductID SKU Price Barcode
1 12 Th-Sm-Red-Cot 120.00 345423
2 12 Th-Sm-Red-Lon 130.00 345454
3 12 Th-Sm-Blue-Cot 140.00 345451
4 12 Th-Sm-Blue-Lon 150.00 345431
5 12 Th-Md-Red-Cot 160.00 345472
6 12 Th-Md-Red-Lon 170.00 345479
7 12 Th-Md-Blue-Cot 180.00 654353
8 12 Th-Md-Blue-Lon 190.00 254353
VariantOptionCombination表:
VariantOptionID SkuID
1 1
3 1
5 1
1 2
3 2
6 2
1 3
4 3
5 3
1 4
4 4
6 4
我的多对多表VariantOptionCombination由Entity Framework自动创建。所以我不能在我的背景下拥有它。
我想要运行的查询是:
var query = from s in YourDbContext.DbSet<ProductSKU>
join voc in YourDbContext.DbSet<VariantOptionCombination> on s.SkuID equals voc.SkuID
join vo in YourDbContext.DbSet<ProductVariantOption> on voc.VariantOptionID equals vo.VariantOptionID
join v in YourDbContext.DbSet<ProductVariant> on vo.VariantID equals v.VariantID
group new {s,voc, vo, v} by s.Price
into g
select new
{
Price = g.Key,
Size = g.Max(x => x.v.Name == "Size" ? x.vo.VariantOptionName : ""),
Color = g.Max(x => x.v.Name == "Color" ? x.vo.VariantOptionName : ""),
Material = g.Max(x => x.v.Name == "Material" ? x.vo.VariantOptionName : "")
};
但我没有dbcontext中的VariantOptionCombination。我首先使用实体框架代码。