如何应用具有多个选项的过滤器

耶塞布

我有这个对象,我试图在C#中使用LINQ来应用过滤器,而我只是很好奇如何应用它。

public class glAccts
{
   public string fund {get;set;}
   public string location {get;set;}
   public string costCenter {get;set;}
   public string object {get;set;}
}

我想做的是可以有如下方法

public async TaskIEnumerable<<glAccts>> applyFilter(IEnumerable<glAccts> filterList)
{
   ... code to fetch a list of all glAccts -- glUserAccess --...
               if (filteredGL.Count() > 0)
        {
            for( int i = 0; i < filteredGL.Count(); i++ )
            {
                if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).fund)) {
                    var response = glUserAccess.Where(c => c.fund.Contains(filteredGL.ElementAt(i).fund));
                }
                if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).location)) {
                    var response = glUserAccess.Where(c => c.location.Contains(filteredGL.ElementAt(i).location));
                }
                if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).costCenter)) {
                    var response = glUserAccess.Where(c => c.costCenter.Contains(filteredGL.ElementAt(i).costCenter));
                }
                if ( !string.IsNullOrEmpty(filteredGL.ElementAt(i).objects)) {
                    var response = glUserAccess.Where(c => c.objects.Contains(filteredGL.ElementAt(i).objects));
                }

            }
        }

}

注意:这是我目前拥有的,但是我不确定如何合并我的响应,以及我的对象是否为空白该怎么办。我还假设使用LINQ可能会有更简单的方法。

我想做的是构造这样的东西:

SELECT glAccts WHERE (activeGlAccts.fund == filteredList[i].fund AND activeGlAccts.location == filteredList[i].location activeGlAccts.costCenter == filteredList[i].costCenter AND activeGlAccts.object == filteredList[i].object ) OR (activeGlAccts.fund == filteredList[i].fund AND activeGlAccts.location == filteredList[i].location activeGlAccts.costCenter == filteredList[i].costCenter AND activeGlAccts.object == filteredList[i].object )

注意:每个过滤的选项之间将存在一个OR,并且每个元素将按AND分组

磁控管

尝试这个:

public async TaskIEnumerable<glAccts> applyFilter(IEnumerable<glAccts> filterList)
{
   //... code to fetch a list of all glAccts -- glUserAccess --...
    var response = glUserAccess.Where(c => 
        filteredGL.Any(x=>
            (string.IsNullOrEmpty(x.fund) || c.fund.Contains(x.fund)) && 
            (string.IsNullOrEmpty(x.location) || c.location.Contains(x.location)) && 
            (string.IsNullOrEmpty(x.costCenter) || c.costCenter.Contains(x.costCenter)) && 
            (string.IsNullOrEmpty(x.objects) || c.objects.Contains(x.objects))
            )
        );
    //... rest of code
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将过滤器应用于具有多个“ AND”条件的DataView

无法使用具有多个条件语句的过滤器从数组中删除元音

在 tableau 中使用具有多个值的列创建过滤器

如何创建具有多个AND条件的过滤器?

如何创建具有多个过滤器的搜索栏?

如何为熊猫数据帧的每一列应用具有不同时间常数的一阶过滤器?

使用具有多个 order by 和 distinct 的 Q 过滤器时,Django 按问题排序

具有多个过滤器的过滤表

具有多个过滤器或多个BroadcastReceiver的BroadcastReceiver?

具有多个集合的Laravel多个过滤器表

在具有多个对象的数组中应用数组过滤器时出错

具有预选项目的jQuery过滤器

具有multiselect选项的jQuery过滤器

如何在具有多个条件数组的javascript中链接多个过滤器函数

如何使用具有日期过滤器的IF else创建新列(然后替换为SAS)

如何正确拥有具有多个过滤器的搜索页面?

具有多个过滤器值的AngularJS过滤器(onclick)

具有多个参数的AngularJS过滤器

具有多个匹配项的JMESPath JSON过滤器

jQuery过滤器具有多个条件的类别

具有多个参数的JQ选择过滤器

具有多个参数的 Wordpress 过滤器

具有多个条件的过滤器阵列

MySQL获取具有多个过滤器值的产品

具有相同网址映射的多个过滤器

具有多个参数的过滤器结构

Excel:具有多个条件的过滤器:AutoFilter或AdvancedFilter

具有多个参数的角度输入过滤器

具有多个条目的eq过滤器行为