C#过滤对象集合

标签 c# entity-framework

<分区>

我正在编写一个方法,它根据以下过滤器返回 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。某种通用的方式? :-)

提前致谢。

最佳答案

是的,绝对有更好的方法。您应该使用在 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(仅事件),null(全部)。

关于C#过滤对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34288871/

相关文章:

c# - byte[] 到 ArrayList?

c# - Entity Framework (6.1.3) 更新未反射(reflect)在 DataContext 中

c# - EF 6 从没有导航属性的其他表中选择

C# 清理文件名

c# - 在Azure中找不到WebJob的日志文件

c# - 通过查询结果阻止访问

entity-framework - Multi-Tenancy 应用程序和 Entity Framework

c# - Entity Framework : C# Winforms bindingsource delete on datagridview, 但仅将 isDeleted 字段标记为真(不删除)

c# - C# 中的类声明

c# - OpenXml 和无法创建互斥量