根据不同的类别从数据库中获取数据

时间:2019-05-18 11:55:37

标签: sql entity-framework linq

我正在设置查询,该查询基于用户选择的过滤器从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表中的产品图像。 SaveedProduct表中有多少人保存了此产品。 仅当用户选择红色,耐克品牌,棉质织物等用户选择了所有过滤器时,它才会返回产品。如果缺少一个,则此查询不返回任何内容。我想要丢失1或2个对象,而不是根据其他选定的钳工返回数据。 如果有任何错误,请原谅我是新蜜蜂。 我错过了参加。

1 个答案:

答案 0 :(得分:0)

这可能会帮助:-

注意:我假设其他连接表中有ProductID,通常,如果您的模型具有Product图像的导航属性并保存产品,则不需要联接。

 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语句。

相关问题