LINQ过滤结果基于另一个列表之间的值

时间:2016-03-01 07:17:58

标签: c# linq

我有一个奇怪的问题,我需要根据价格范围过滤搜索结果。所以我有两个清单:

列出A ,其中包含所有价格的商品。

列表B 包含所有价格范围,例如价格b / w 20和30以及50-60之间的价格。

现在我需要在A中过滤结果,其中价格介于列表B中指定的价格范围之内。

我的linq查询是:

A - 产品数据

var list = (from a in db.MItems
                        join b in db.DCategoryProducts on a.ProductCode equals b.ProductCode
                        join c in db.DItemTargetAreas on a.ProductCode equals c.ProductCode
                        join d in db.DItemQuantities on a.ProductCode equals d.ProductCode
                        where a.IsActive && b.ItemCategoryCode == itemCategoryID
                        && !string.IsNullOrEmpty(_targetarea) ? _targetarea.Contains(c.TargetArea.ToString()) : c.TargetArea == c.TargetArea
                        && !string.IsNullOrEmpty(_compression) ? _compression.Contains(a.CompressionRating.ToString()) : a.CompressionRating == a.CompressionRating
                        && !string.IsNullOrEmpty(_color) ? _color.Contains(d.ColourCode.ToString()) : d.ColourCode == d.ColourCode
                        && !string.IsNullOrEmpty(_style) ? _style.Contains(d.StyleCode.ToString()) : d.StyleCode == d.StyleCode
                        && !string.IsNullOrEmpty(_size) ? _size.Contains(d.SizeCode.ToString()) : d.SizeCode == d.SizeCode


                        && a.Price between my price_ranges


                        orderby a.ProductName
                        select new Models.ViewModels.Product
                        {
                            ItemCategoryCode = a.ItemCategoryCode,
                            ProductCode = a.ProductCode,
                            ProductName = a.ProductName,
                            SmallPic = a.SmallPic,
                            Price = a.Price,
                            PriceWas = a.PriceWas,
                            IsNew = a.IsNew
                        }).Distinct().ToList();

B - 价格范围

var priceRanges = (from p in db.DPriceSlabs
                               where !string.IsNullOrEmpty(_priceSlab) ? _priceSlab.Contains(p.PRICE_ID.ToString()) : p.PRICE_ID == p.PRICE_ID
                               select new { p.PRICE_FROM, p.PRICE_TO }).ToList();

如何通过应用列表B 中的过滤器,从列表A 中获得所需结果?对当前查询的任何改进也将非常感激。

TIA

1 个答案:

答案 0 :(得分:6)

你想要这样的东西吗?

list.Where(item => priceRanges.Any(price => item.Price >= price.PRICE_FROM && item.Price <= p.PRICE_TO ));
相关问题