LINQ 根据另一个列表之间的值过滤结果[英] LINQ Filter results based on values between another list

本文是小编为大家收集整理的关于LINQ 根据另一个列表之间的值过滤结果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个特殊的问题,我需要根据价格范围过滤搜索结果.所以我有2个列表:

列出包含所有物品的价格.

列表b 包含所有价格范围,例如价格b/w 20和30,价格在50-60之间.

现在,我需要在列表中指定的价格范围之间过滤结果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();

如何通过应用 list b 的过滤器来从列表a 中获得所需的结果?当前查询的任何改进也将不胜感激.

tia

推荐答案

您想要这样的东西吗?

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

本文地址:https://www.itbaoku.cn/post/1556770.html

问题描述

I have a peculiar problem where I need to filter my search results based on price ranges. So I have 2 lists:

List A containing all items with their price.

List B containing all price ranges like price b/w 20 and 30 and price between 50-60.

Now I need to filter results in A where price lies between the price ranges specified in list B.

My linq queries are:

A - Product Data

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 - Price Ranges

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();

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

推荐答案

You want something like this ?

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