Contains("") 是否有等效的“*”?我正在使用一些通配符进行过滤,如果没有应用过滤器,那么我需要全部返回吗?
string[] filter = {1,2}; // This is dynamic could be filtered values or {} empty.
// This works for filtering by products (1,2)
db.Products.Where(x => filter.Contains(x.ProdId));
我真正需要实现的是这样的:
// If the filter is empty get all results...if there is a filter passed the filter values in for select
db.Products.Where(x => x.ProdId.Contains(filter.Length == 0 ? "*" : filter);
最佳答案
如有必要,您可以只添加位置:
var query = db.Products.AsQueryable();
if (filter.Any())
query = query.Where(x => filter.Contains(x.ProdId));
// use query as needed
关于c# - LINQ/Projection - 包含用于过滤的所有通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8434870/