LINQ根据另一个列表之间的值过滤结果 [英] LINQ Filter results based on values between another list
问题描述
我有一个特殊的问题,我需要根据价格范围过滤搜索结果.所以我有2个列表:
I have a peculiar problem where I need to filter my search results based on price ranges. So I have 2 lists:
列表A 包含所有项目及其价格.
List A containing all items with their price.
列表B ,其中包含价格范围20/30和价格50-60之间的所有价格范围.
List B containing all price ranges like price b/w 20 and 30 and price between 50-60.
现在我需要过滤A中价格在列表B中指定的价格范围之间的结果.
Now I need to filter results in A where price lies between the price ranges specified in list 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 中获得所需的结果?对当前查询的任何改进也将不胜感激.
How can I get the desired results out of List A by applying filters from List B ? Any improvements to the current queries will also be much appreciated.
TIA
推荐答案
您想要这样的东西吗?
list.Where(item => priceRanges.Any(price => item.Price >= price.PRICE_FROM && item.Price <= p.PRICE_TO ));
这篇关于LINQ根据另一个列表之间的值过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!