How to use conditional aggregation sql query in Entity Framework?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am using Asp.Net MVC 5 and Entity Framework 6.2.0 with LINQ Extension Methods syntax.

I have 5 tables of variants, details below:

Product Table:

ProductID Name     12    T-Shirt 

Variant Table:

VariantID  ProductID  Name     1         12      Size     2         12      Color     3         12      Material 

VariantOption Table:

VariantOptionID  VariantID  VariantOptionName       1              1            Small       2              1            Medium       3              2            Red       4              2            Blue       5              3            Cotton       6              3            Lawn 

Sku Table:

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 Table:

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 

I want to show these tables records on web page as.

Size    Color  Material  Price   Sku Small   Red    Cotton    120.00  345423 Small   Red    Lawn      130.00  345454 Small   Blue   Cotton    140.00  345451 Small   Blue   Lawn      150.00  345431 Medium  Red    Cotton    160.00  345472 Medium  Red    Lawn      170.00  345479 Medium  Blue   Cotton    180.00  654353 Medium  Blue   Lawn      190.00  254353 

I am using this query to achieve the desire output. How can i translate this query in Entity Framework linq?

select  max(case when v.Name = 'Size' then vo.Name end) as Size,     max(case when v.Name = 'Color' then vo.Name end) as Color,     max(case when v.Name = 'Material' then vo.Name end) as Material,     s.price from ProductSKU s join ProductVariantOptionCombination voc on s.SkuID = voc.SkuId join ProductVariantOption vo on vo.VariantOptionId = voc.VariantOptionId   join ProductVariant v on v.VariantId = vo.VariantId group by s.Price; 

回答1:

Try like this;

        var query = from s in YourDbContext.DbSet<ProductSKU>             join voc in YourDbContext.DbSet<ProductVariantOptionCombination> 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 : "")             }; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!