linq - PredicateBuilder 在 Linq 中查询逗号分隔的字符串
问题描述
我正在尝试使用 PredicateBuilder 查询逗号分隔的字符串。我的数据集有 TR 格式的 DAYS,表示周二和周四,但我的参数查询字符串将以这种方式传递:Day=T,R。我的查询现在没有返回任何数据,所以我想必须使用 like '%' 来获取数据。谁能告诉我下面的 linq 查询中的问题出在哪里:
public List<myTable> Get(string filter1 = null, string Day = null)
{
if (string.IsNullOrWhiteSpace(filter1) && string.IsNullOrWhiteSpace(Day) )
return new List<myTable>();
IQueryable<myTable> qry = db.myTable.AsQueryable();
var searchPredicate = PredicateBuilder.True<myTable>();
if (filter1 !=null){
searchPredicate = searchPredicate.And(a => a.Column1.Contains(filter1)
}
if (Day != null)
{
//split day string
string[] items = Day.Split(',');
foreach (var item in items)
{
searchPredicate = searchPredicate.And(a => items.Contains("%" + a.DAYS + "%"));
}
}
return qry.where(searchPredicate).ToList();
}
或者,如何创建这样的 where 谓词: where column1='filter' and (DAYS like 'T%' or DAYS like 'M%') ?谢谢。
解决方案
推荐阅读
- excel - 自动过滤仅返回两个数字之间的数据,但返回空白
- firebase - 参数不匹配的闭包调用:颤振中的函数'[]'
- c++ - 广播类似于 Numpy 的犰狳矩阵运算的最佳方式
- android - 如何在 ViewHolder Android Studio 中设置可绘制背景?
- c# - 检查是随机创建的添加的答案在 C# 中是否正确
- firebase - 同时使用实时数据库和 Firestore
- bash - 如何制作循环将 groff 文档编译到特定文件夹中
- c - 如何将 typedef 变量用作函数的参数
- python - 在 Python 中直接从 Web 读取 txt 文件
- apache-spark - Spark SQL 中 Encoders.kryo[T] 和 Encoders.product[T] 的区别