How to use conditional aggregation sql query in Entity Framework?

前端 未结 1 720
南方客
南方客 2021-01-28 06:19

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:



        
相关标签:
1条回答
  • 2021-01-28 07:03

    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 : "")
                };
    
    0 讨论(0)
提交回复
热议问题