c# - LINQ/Projection - 包含用于过滤的所有通配符

标签 c# linq linq-to-sql contains

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/

相关文章:

c# - 如何更改 HttpClient 响应的编码

c# - 通过 LINQ 中的属性缓冲表达式(对比 Lambda)

c# - 我什么时候应该在 IEnumerable<T> 的上下文中使用 .Count() 和 .Count

SQL RANDOM ORDER BY 连接表

c# - 在链接构造函数时初始化 Dictionary<string, string>

c# - 如何将 System.Net.ICredentials 传递给 API?

asp.net-mvc - 将序列化的 JQuery 可排序结果提交到数据库的最佳方法是什么?

c# - 如何使用通用 LINQ to SQL 存储库的更新方法

c# - DataBinding/WPF C# 的通用 Observable 字典类

c# - 排列是否存在