使用 linq select 多个价格值

Using linq select a multiple price values

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

您可以像这样使用 cross join 并得到 IEnumerable<FilterModel>

var distinctNewFilterModel = from filter in filterModel
                             from price in priceList
                             where filter.Price > price.MinPrice && filter.Price < price.MaxPrice
                             group filter by filter.Price into groupped
                             select groupped.First();

但不确定这个最短最干净

我不知道这对你来说是否足够简洁明了,但是...

var newFilterModel = filterModel
    // Select just the price
    .Select(f => f.Price)
    // Remove duplicates
    .Distinct()
    // Find prices in the price list
    .Where(price => priceList
        .FindIndex(p => p.MinPrice <= price && price <= p.MaxPrice) != -1)
    // Turn the price back into a FilterModel object
    .Select(price => new FilterModel { Price = price })
    // Turn the entire result into a new List<FilterModel>
    .ToList();

newFilterModel.ForEach(newF => Console.WriteLine(newF.Price));

结果:

1
15
20
410
555

如果您要像这样在 FilterModel class 中实施 IEquatable<>

public class FilterModel : IEquatable<FilterModel>
{
    public int Price { get; set; }

    public bool Equals(FilterModel other)
    {
        //Check whether the compared object is null.
        if (ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data.
        if (ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal.
        return other.Price == Price;
    }

    public override int GetHashCode()
    {
        //Get hash code for the Price field.
        return Price.GetHashCode();
    }
}

然后您的 Linq 语句变得更短:

var newFilterModel = filterModel
    // Remove duplicates
    .Distinct()
    // Find prices in the price list
    .Where(filterPrice => priceList
        .FindIndex(price => price.MinPrice <= filterPrice.Price && filterPrice.Price <= price.MaxPrice) != -1)
    // Turn the entire result into a List<FilterModel>
    .ToList();

newFilterModel.ForEach(p => Console.WriteLine(p.Price));

结果:

1
15
20
410
555