首页 > 解决方案 > 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%') ?谢谢。

标签: linqpredicatebuilder

解决方案


推荐阅读