【问题标题】:C# filtering a collection of objects [duplicate]C#过滤对象集合[重复]
【发布时间】:2015-12-15 12:07:29
【问题描述】:

我正在编写一个基于以下过滤器返回 ProductPeriod 对象集合的方法:

DateTime? from
DateTime? to
bool? includeActive
bool? includeInactive

ProductPeriod 对象如下所示:

public class ProductPeriod
{
    public int Id { get; set; }
    public string Name
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public bool IsActive { get; set; }
}

所以这个想法是,客户可以选择一个截止日期和/或一个起始日期和/或包括活跃期和/或包括非活跃期。这为过滤提供了相当多的场景,这构成了一个相当大的方法,我开始编写(但还没有完成):

public IEnumerable<ProductPeriod> GetFilteredProductPeriods(DateTime? from,     DateTime? to, bool? includeActive, bool? includeInactive)
{            
    // 1. from date only
    if(from.HasValue && to == null && includeActive == null && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.StartDate >= from.Value).ToList();

    // 2. from and to date
    if(from.HasValue && to.HasValue && includeActive == null && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.StartDate >= from.Value && x.EndDate <= to.Value).ToList();

    // 3. to date only
    if (to.HasValue && from == null && includeActive == null && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.EndDate <= to.Value).ToList();

    // 4. from date and show active
    if (from.HasValue && (includeActive != null && includeActive.Value) && to == null && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.StartDate >= from.Value && x.IsActive).ToList();

    // 5. from, to and show active
    if (from != null && to != null && (includeActive != null && includeActive.Value) && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.StartDate >= from.Value && x.EndDate <= to.Value && x.IsActive).ToList();

    // 6. to date and show active
    if (to.HasValue && (includeActive != null && includeActive.Value) && from == null && includeInactive == null)
        return _entities.ProductPeriods.Where(x => x.EndDate <= to.Value && x.IsActive).ToList();

    // 7. .... and so on, so forth..
}

我想知道是否有更好/更智能的方法来做到这一点,我不知道? IE。某种通用方式? :-)

提前致谢。

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

是的,肯定有更好的方法。您应该使用可以在 LINQ 中构建查询的方式:

public IEnumerable<ProductPeriod> GetFilteredProductPeriods
    (DateTime? from, DateTime? to, bool? includeActive, bool? includeInactive)
{
    IQueryable<ProductPeriod> query = _entities.ProductPeriods;
    if (from != null)
    {
        query = query.Where(x => x.StartDate >= from.Value);
    }
    if (to != null)
    {
        query = query.Where(x => x.EndDate >= to.Value);
    }
    if (includeActive == false)
    {
        query = query.Where(x => !x.IsActive);
    }
    if (includeInactive == false)
    {
        query = query.Where(x => x.IsActive);
    }
    return query.ToList();
}

请注意,设置 includeInactive=falseincludeActive=false 不会给您任何结果...您可能希望将其更改为单个参数,即 false(仅非活动)、true(仅活动)、@ 987654326@(全部)。

【讨论】:

  • 太棒了! :-) 非常感谢乔恩。今天学到了新东西!
  • @Silvermind:不,我有includeInactive == falseincludeActive == false。对于from.Value 部分......可能,但我认为这样更清楚。
  • 我完全阅读了它。这对我来说似乎模棱两可。
猜你喜欢
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多