我想根据字符串列表过滤结果,像这样:
List<string> filters = {"a", "b", "c", "d"};
var results = (from R in db.Entries
where R.word.StartsWith(filters[0])
||R.word.StartsWith(filters[1])
||R.word.StartsWith(filters[2])
||...
我不知道我的过滤器列表的长度,所以如何在 LINQ 中动态查询它?
提前致谢。
最佳答案
你应该可以这样做:
var results = db.Entries
.Where(r => filters.Any(f => r.word.StartsWith(f)));
扩展方法Any
是一种将应用于列表的 OR ||
链“折叠”到单个调用中的方法。
关于c# - 使用字符串列表过滤 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316504/