【问题标题】:Fetching data from the database based on different categories根据不同类别从数据库中获取数据
【发布时间】:2019-05-18 11:55:37
【问题描述】:

我正在设置查询,该查询根据用户选择的过滤器从 3 个表中获取数据。过滤器可以有多个,也可以没有。

我制作了不同组合的存储过程。但我知道那是我做过的最糟糕的事情。

var result =  (from product in context.Products
               from img in context.ProductImage
               from saved in context.SavedProduct
               where (cat.Color.Contains(product.Color)
                & cat.BrandName.Contains(product.Brand_Name)
                & cat.Fabric.Contains(product.Fabric)
                & cat.Design.Contains(product.Design)) 
                select new
                {
                   product.ProductID,
                   product.Price,
                   product.Brand_Name,
                   product.Title,
                   product.Color,
                   product.Fabric,
                   product.Design,
                   img.Image,
                   saved.ProductSavedCounter,
                }).ToList();

产品表中的产品相关详细信息。 ProductImage 表中的产品图像。 SavedProduct 表中有多少人保存了此产品。 仅当用户选择所有过滤器时才返回产品,即当用户选择红色、耐克品牌、棉织物等时。如果缺少一个,则此查询不返回任何内容。我希望当错过 1 或 2 时,它应该根据其他选定的装配工返回数据。 如果有任何错误,请原谅我,我是新蜜蜂。 我错过了连接。

【问题讨论】:

    标签: sql entity-framework linq


    【解决方案1】:

    这可能会有所帮助:-

    注意:我假设 ProductID 在其他表中以进行连接,一般来说,如果您的模型具有产品图像的导航属性并保存产品,则您不需要连接。

     var result = from product in context.Products
                             join img in context.ProductImage on product.ProductID equals img.ProductID
                             join saved in context.SavedProduct on product.ProductID equals saved.ProductID
                             where (
                               (string.IsNullOrEmpty(cat.Color) || cat.Color.Equals(product.Color))
                             & (string.IsNullOrEmpty(cat.BrandName) || cat.Brand_Name.Equals(product.Brand_Name))
                             & (string.IsNullOrEmpty(cat.Fabric) || cat.Fabric.Equals(product.Fabric))
                             & (string.IsNullOrEmpty(cat.Design) || cat.Design.Equals(product.Design))
                             & (
                              ((string.IsNullOrEmpty(cat.Color) ? 1 : 0) +
                              (string.IsNullOrEmpty(cat.BrandName) ? 1 : 0) +
                              (string.IsNullOrEmpty(cat.Fabric) ? 1 : 0) +
                              (string.IsNullOrEmpty(cat.Design) ? 1 : 0)) >= 2) //at least two conditions
                              )
                            select new {
                                product.ProductID,
                                product.Price,
                                product.Brand_Name,
                                product.Title,
                                product.Color,
                                product.Fabric,
                                product.Design,
                                img.Image,
                                saved.ProductSavedCounter,
                            };
    

    我认为更好的解决方案是逐步构建它,如下所示:-

     //build the joins
            var result = (from product in context.Products
                          join img in context.ProductImage on product.ProductID equals img.ProductID
                          join saved in context.SavedProduct on product.ProductID equals saved.ProductID
                          select new { product, img, saved }).AsQueryable();
    
            //get number of conditions avaliable
            var conditionCount = (string.IsNullOrEmpty(cat.Color) ? 1 : 0) +
                          (string.IsNullOrEmpty(cat.Brand_Name) ? 1 : 0) +
                          (string.IsNullOrEmpty(cat.Fabric) ? 1 : 0) +
                          (string.IsNullOrEmpty(cat.Design) ? 1 : 0);
    
            if (conditionCount >= 2)
            {
    
                //add the condition if they exists
                if (!string.IsNullOrEmpty(cat.Color))
                    result = result.Where(x => cat.Color.Equals(x.product.Color));
                if (!string.IsNullOrEmpty(cat.Brand_Name))
                    result = result.Where(x => cat.Brand_Name.Equals(x.product.Brand_Name));
                if (!string.IsNullOrEmpty(cat.Fabric))
                    result = result.Where(x => cat.Fabric.Equals(x.product.Fabric));
                if (!string.IsNullOrEmpty(cat.Design))
                    result = result.Where(x => cat.Design.Equals(x.product.Design));
    
                //make the final select
                var finalResult = result.Select(x => new
                {
                    x.product.ProductID,
                    x.product.Price,
                    x.product.Brand_Name,
                    x.product.Title,
                    x.product.Color,
                    x.product.Fabric,
                    x.product.Design,
                    x.img.Image,
                    x.saved.ProductSavedCounter,
                }).ToList();
            }
    

    如果你的意思是当任何过滤器丢失时,查询应该忽略它,删除条件计数和它的 if 语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多