Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据选中的多个复选框筛选列表的最佳方法_C# - Fatal编程技术网

C# 根据选中的多个复选框筛选列表的最佳方法

C# 根据选中的多个复选框筛选列表的最佳方法,c#,C#,目前,我得到了可能的组合数(复选框数的阶乘),并编写了许多if语句,如: 假设我有3个复选框: 尽管这将满足要求,但考虑到未来复选框的数量可能会增加,并且组合的数量可能会增加,我不认为这是一个最佳解决方案 我一直在想,当根据所选复选框筛选列表时,是否存在已知模式?将布尔值与每个字段进行比较。 试试这个: return _callsData.Where(x => x.IncomingCall == IncludeIncomingCalls && x.OutgoingCall

目前,我得到了可能的组合数(复选框数的阶乘),并编写了许多if语句,如:

假设我有3个复选框:

尽管这将满足要求,但考虑到未来复选框的数量可能会增加,并且组合的数量可能会增加,我不认为这是一个最佳解决方案


我一直在想,当根据所选复选框筛选列表时,是否存在已知模式?

将布尔值与每个字段进行比较。
试试这个:

return _callsData.Where(x => x.IncomingCall == IncludeIncomingCalls  && x.OutgoingCall == IncludeOutgoingCalls && x.ExternalCall== IncludeExternalCalls);
试试这个:

return _callsData.Where(x => x.IncomingCall==IncludeIncomingCalls  && x.OutgoingCall==IncludeOutgoingCalls  && x.ExternalCall==IncludeExternalCalls);

请注意,您可以编写
IQueryable
。您可以根据需要添加附加的
Where
子句

var result = callsData.Select(x => x);

if (IncludeIncomingCalls) {
    result = result.Where(x => x.IncomingCall);
}
else {
    result = result.Where(x => !x.IncomingCall);
}

if (IncludeOutgoingCalls) {
    result = result.Where(x => x.OutgoingCall);
}
else {
    result = result.Where(x => !x.OutgoingCall);
}

if (IncludeExternalCalls) {
    result = result.Where(x => x.ExternalCall);
}
else {
    result = result.Where(x => !x.ExternalCall);
}

return result;
我只是将其作为一般模式显示。对于您的用例,无处不在的开发人员的解决方案更容易阅读和理解

但是,如果条件比一个位标志更复杂,这个模式可能会派上用场。举个例子:

if (ShowOnlyActive) {
    result = result.Where(x => x.State == CallState.Active);
}
else {
    result = result.Where(x => x.State == CallState.Deleted || x.State == CallState.Inactive);
}
脱离主题,只是为了进一步说明这个一般概念:向
IQueryable
添加附加子句可用于将查询的部分重构为帮助程序或扩展方法,例如实现分页

public interface IPageableQuery {
    // The page size (i.e. the number of elements to be displayed).
    // The method processing the Query will Take() this number of elements.
    int DisplayLength { get; set; }

    // The number of elements that have already been displayed.
    // The method processing the Query will Skip() over these elements.
    int DisplayStart { get; set; }
}

public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> entries, IPageableQuery query)
    where T : class {
    if (query.DisplayStart >= 0 && query.DisplayLength > 0) {
        return entries.Skip(query.DisplayStart).Take(query.DisplayLength);
    }
    return entries;
}
公共接口IPageableQuery{
//页面大小(即要显示的元素数)。
//处理查询的方法将接受()此数量的元素。
int DisplayLength{get;set;}
//已显示的元素数。
//处理查询的方法将跳过()这些元素。
int DisplayStart{get;set;}
}
公共静态IQueryable应用程序分页(此IQueryable条目,IPageableQuery查询)
T:在哪里上课{
如果(query.DisplayStart>=0&&query.DisplayLength>0){
返回条目.Skip(query.DisplayStart).Take(query.displayslength);
}
返回条目;
}

您可以使用以下模式:

//some checkboxes
CheckBox chkA = ...
CheckBox chkB = ...
CheckBox chkC = ...

//build up your filters
var filters = new List<Predicate<SomeEntity>>();
filters.Add(e => chkA.Checked && e.IsA);
filters.Add(e => chkB.Checked && e.IsB);
filters.Add(e => chkC.Checked && e.IsC);

//And now simply apply the filters
var entities = ... //some enumerable of SomeEntity
var filteredEntities = entities.Where(e => filters.All(filter => filter(e)));
//一些复选框
复选框chkA=。。。
复选框chkB=。。。
复选框chkC=。。。
//建立你的过滤器
var filters=新列表();
filters.Add(e=>chkA.Checked&&e.IsA);
filters.Add(e=>chkB.Checked&&e.IsB);
filters.Add(e=>chkC.Checked&&e.IsC);
//现在只需应用过滤器
变量实体=//某个实体的某个可枚举项
var filteredEntities=entities.Where(e=>filters.All(filter=>filter(e));
请注意,只有当
IsA
IsB
IsC
排除了这些条件时,这才可以正常工作,但这似乎是您当前的设置

//some checkboxes
CheckBox chkA = ...
CheckBox chkB = ...
CheckBox chkC = ...

//build up your filters
var filters = new List<Predicate<SomeEntity>>();
filters.Add(e => chkA.Checked && e.IsA);
filters.Add(e => chkB.Checked && e.IsB);
filters.Add(e => chkC.Checked && e.IsC);

//And now simply apply the filters
var entities = ... //some enumerable of SomeEntity
var filteredEntities = entities.Where(e => filters.All(filter => filter(e)));