I Have a model in my Web-Application like this:
class MyClass
{
int ID;
List<AnotherClass> foo {get; set;}
}
class AnotherClass
{
int ID;
string bar;
}
I also have a list of filter-strings given by the user.
List<string> filter = new List<string> { "foo", "bar" } //as example
What I want is to filter MyClass
by the given strings like this:
var result = context.MyClass.Include(mc => mc.foo);
result = result.Where(x => filter.Any(f => f == x.foo.Select(d => d.bar)));
The Problem is: Select()
returns a List of strings and I can't compare a string with a list of strings.
Anyone an idea how to fix this issue?
使用Contains
result = result.Where(x => x.foo.Any(i => filter.Contains(i.bar)));
If putting into words what you want is: Find whichever item in result, if any of its inner class items is present in the filter list
Try:
result.Where(item => item.Any(innerItem => filter.Contains(innerItem.bar)))
Or in the other syntax:
var output = (from item in result
from innerItem in item.foo
where filter.Contains(innerItem.bar)
select item);
您可能需要简单的Any
和Contains
方法,就可以做到这一点。
result.Where(x => x.foo.Any(f=> filter.Contains(f.bar));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.