0

I am trying to query a comma delimited string using PredicateBuilder. My data set has DAYS in TR format which means Tuesday and Thursday, but my parameter query string will be passed in this way: Day=T,R. My query does not return any data right now so I guess have to use like '%' to get data. Can anyone tell me where the problem is in my linq query below:

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();

 }

Or, how to create a where Predicate like this: where column1='filter' and (DAYS like 'T%' or DAYS like 'M%') ? Thanks.

user788448
  • 779
  • 1
  • 14
  • 36
  • 1
    You need to leave off the `%` - the `Contains("x")` method is translated to `LIKE '%x%'` by LINQ. To do just `T%` you would need to use `StartsWith`. – NetMage Jul 29 '19 at 19:40
  • Thanks. How to create the predicate inside the foreach loop searchPredicate= searchPredicate.Or(a=>a.DAYS.StartsWith(item) and outside foreach to use searchPredicate=searchPredicate.And()? – user788448 Jul 29 '19 at 21:53
  • 2
    Found the answer here https://stackoverflow.com/questions/45268997/predicatebuilder-or-condition-nested-inside-and . Create an inner predicate Or and then And – user788448 Jul 30 '19 at 02:28

0 Answers0