使用linq选择多个价格值

时间:2015-07-28 12:54:57

标签: linq select where addrange

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

2 个答案:

答案 0 :(得分:2)

我不知道这是否短暂&amp;对你来说足够干净,但是......

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

如果您要在IEquatable<>课程中实施FilterModel,请执行以下操作:

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

答案 1 :(得分:1)

您可以像这样使用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();

但不确定这个最短最干净比你有