-3
var predicate = PredicateBuilder.New<Items>(true);
predicate = predicate.Or(i => Convert.ToDateTime(i.Entrydate).ToString("M/d/yyyy").Contains(searchText));`

Entrydate is DateTime? type so directly I can't able to use .ToString('M/d/yyyy') string format.

This is not working (exception : Must be reducible node)

IQueryable<Items> items = Repository.GetItems(orderNo).Where(predicate);

This is working

IQueryable<Items> items = Repository.GetItems(orderNo).ToList().Where(predicate).AsQueryable();

But I don't want to convert the results to list while these are in IQueryable

Please note: I am using Convert.ToDateTime() method in predicate (dynamic) where condition

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hi! Add your predicate, so we could see what are you trying to achieve. – Morasiu Feb 21 '19 at 11:35
  • 1
    Can you elaborate on 'not working'? Is there an error? If so, what is it? And what is `predicate`? – Charles Mager Feb 21 '19 at 11:35
  • 1
    What means "not working" ? An Error message? In NHibernate ORM there was queries that the ORM couldn't map to SQL query because of it's complexity. – koviroli Feb 21 '19 at 11:39
  • Something in the predicate code requires the List. You probably need to change a variable definition in the predicate from List<> to var. You also need to change the return type. Note you cannot return a var object you have to cast return item to var. – jdweng Feb 21 '19 at 11:40
  • `AsQueryable` should be removed - it is very rarely useful. – mjwills Feb 21 '19 at 11:53
  • 3
    Please always include full exception messages. You get a `NotSupportedException`, because `Convert.ToDateTime()` can't be translated into SQL. You should look for an alternative that can be translated. Why do you need the conversion anyway? – Gert Arnold Feb 21 '19 at 11:54
  • Hi I have been added predicate code and exception so that you can find it out. – Rajsekhar Patnaik Feb 21 '19 at 12:31
  • I need to use `Convert.ToDateTime()` because the datatype i have is `DateTime?`. So that in this type of datatype i can't able to use `.ToString("M/d/yyyy")` format. – Rajsekhar Patnaik Feb 21 '19 at 12:36
  • Please share some example values of `searchText`. – mjwills Feb 21 '19 at 12:51
  • Neither `Convert` nor `ToString(format)` are supported by EF SQL query translator. Btw, `Must be reducible node` exception sounds like EF **Core**, is that correct? If yes, it's good to include such information in the question and tags. Also the version. – Ivan Stoev Feb 21 '19 at 13:10
  • `searchText` may be `searchText = "2/"`, `searchText = "2/2/20"` etc – Rajsekhar Patnaik Feb 21 '19 at 14:36

1 Answers1

2

So you have a class Items, with a DateTime? property EntryDate. And you need as input for your predicate builder an `Expression> that represents a predicate that returns true if the string representation of Items.EntryDate contains some searchText.

In other words, if EntryDate equals "23 december 2003", then its string representation is "12/25/2003" and you want to return true if this contains searchText.

Alas you didn't specify what you want if EntryDate is null. I assume you want to return false.

Expression<Func<Items, bool>> predicate = (items.EntryDate != null)
   && (items.EntryDate.ToString("M/d/yyyy").Contains(searchText));

You can use this in your PredicateBuilder.

By the way: I think it is not wise to compare textual representations of DateTime. This is way to Culture dependent.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116