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

时间:2019-03-19 17:50:12

标签: 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<<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分组。

1 个答案:

答案 0 :(得分:1)

尝试一下:

user