var filterModel = new List<FilterModel>()
{
new FilterModel {Price = 1},
new FilterModel {Price = 1},
new FilterModel {Price = 15},
new FilterModel {Price = 20},
new FilterModel {Price = 410},
new FilterModel {Price = 9511},
new FilterModel {Price = 9511},
new FilterModel {Price = 252},
new FilterModel {Price = 555},
new FilterModel {Price = 602}
};
var priceList = new List<PriceList>
{
new PriceList{MinPrice = 0,MaxPrice = 30},
new PriceList{MinPrice = 70,MaxPrice = 130},
new PriceList{MinPrice = 200,MaxPrice = 250},
new PriceList{MinPrice = 400,MaxPrice = 600},
//etc.etc. continue...
};
我有2个模型。我正在尝试使用 LINQ。我的代码正在运行。最短(最干净)的方法是什么?
var newFilterModel = new List<FilterModel>();
foreach (var t in priceList)
{
newFilterModel.AddRange(filterModel
.Where(x => x.Price > t.MinPrice && x.Price < t.MaxPrice)
.ToList());
}
var distinctNewFilterModel = newFilterModel.Select(p=>new { p.Price})
.Distinct().ToList();