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

2 回答 2

2

我不知道这对你来说是否足够短和干净,但是......

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
于 2015-07-28T13:35:30.793 回答
1

您可以像这样使用交叉连接并获得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();

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

于 2015-07-28T13:18:16.903 回答