C# predicate list passed to Linq Where clause

南楼画角 提交于 2019-12-05 03:07:05

You have to loop over your filters and run a test on each one.

You can do it with linq like this to return true if any of your filters are true:

.Where(p => { foreach(f in filters) if (f(p) == true) return(true); return(false)}) 

or like this to to return true if all of your filters are true:

.Where(p => { foreach(f in filters) if (f(p) == false) return(false); return(true)}) 

There are at least two errors in your code:

List<Expression<Func<Note, bool>>> filters = new List<Expression<Func<Note, bool>>>();

change it to

List<Func<Note, bool>> filters = new List<Func<Note, bool>>();

You don't need Expression trees here. You are using IEnumerable<>, not IQueryable<>

notes = dataAccess.GetList<Note>(pn => pn.ProjectVersionID == projectVersionID, filterExtensions.ToArray())
  .Where(filters.ToArray()).Take(10).ToList();

There .Where() accepts a single predicate at a time. You could:

notes = dataAccess.GetList<Note>(pn => pn.ProjectVersionID == projectVersionID, filterExtensions.ToArray())
  .Where(x => filters.All(x)).Take(10).ToList();

or various other solutions, like:

var notesEnu = dataAccess.GetList<Note>(pn => pn.ProjectVersionID == projectVersionID, filterExtensions.ToArray())
              .AsEnumerable();

foreach (var filter in filters)
{
    notesEmu = notesEmu.Where(filter);
}

notes = notesEnu.Take(10).ToList();

Because all the .Where() conditions are implicitly in &&.

I think great answer from Hogan can be simplified and shorten a bit by use of Any and All Linq methods.

To get items that fulfill all the conditions:

var resultAll = listOfItems.Where(p => filters.All(f => f(p)));

And to get the items that fulfill any condition:

var resultAny = listOfItems.Where(p => filters.Any(f => f(p)));

You can't just pass an array of predicates to the where method. You need to either iterate over the array and keep calling Where() for each expression in the array, or find a way to merge them all together into one expression and use that. You'll want to use LinqKit if you go the second route.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!