C# 如何应用具有多个选项的筛选器

C# 如何应用具有多个选项的筛选器,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个对象,我正试图用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

我有一个对象,我正试图用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));
                }

            }
        }

}
注意:每个筛选选项之间都会有一个OR,每个元素都将按分组,请尝试以下操作:

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
}
public异步任务IEnumerable applyFilter(IEnumerable filterList)
{
//…获取所有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))
)
);
//…代码的其余部分
}

您的
中的
子句都是完全相同的(在最后一个代码片段中),我想我想说的是,对于每个IEnumerable FilterAdappy,它不会占用以前的资金、位置、成本中心和对象。我不再熟悉SQL查询了。它几乎可以工作了,您的操作很有意义,但它只获取第一个filteredgl元素,而不是第二个或第三个元素。我认为x.fund和x.location只抓住了第一个filteredGl,而现在它们每个都抓住了。我错了吗?@Jseb你不是想过滤glUserAcess吗?将返回的元素,而不是来自filteredGL的元素。对于每个GluseAccess元素,代码计算整个filteredGL搜索匹配项,如果找到一个,则保留该项,否则将对其进行分类。抱歉,我正在测试,我认为刚刚发现第二个元素不是我原始列表的一部分,因此您的代码是正确的,谢谢。
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
}