3

How can I correctly parse sdate ?

   IQueryable bddata = Junior2KepwareContext.Set(type)
       .Where($"C_NUMERICID == {idLinea}")
       .Where("C_TIMESTAMP > "+ startDate )
       .OrderBy("C_TIMESTAMP");

System.Linq.Dynamic.ParseException: 'Operator '>' incompatible with operand types 'DateTime?' and 'Int32''

enter image description here

enter image description here

Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64

2 Answers2

3

You need to use parameters:

IQueryable bddata = Junior2KepwareContext.Set(type)
   .Where($"C_NUMERICID == {idLinea}")
   // @0 is first parameter in the list, @1 is second etc
   .Where("C_TIMESTAMP > @0", startDate) // startDate is of type DateTime, not string
   .OrderBy("C_TIMESTAMP");

It's a good practice to always use parameters instead of inlining values (so for example use it for idLinea too).

Evk
  • 98,527
  • 8
  • 141
  • 191
  • 1
    @MassimoVariolo well, because it was designed like this I guess. When you use parameters - parser knows their correct types (in this case it sees that `startDate` is `DateTime`) and is able to build correct query. If you don't use parameters - sometimes it can build correct query (if you use plain numbers for example) and sometimes it does not. So to avoid all the problems - just always use parameters. – Evk Feb 07 '18 at 11:37
0

If you really need to use only one string instead of several parameters, you are able to do so.

For your case it would give :

IQueryable bddata = Junior2KepwareContext.Set(type)
   .Where($"C_NUMERICID == {idLinea}")
   .Where($"C_TIMESTAMP > DateTime({startDate.Year},{startDate.Month},{startDate.Day}" )
   .OrderBy("C_TIMESTAMP");

Source : https://zzzcode.ai/answer-question?p1=Entity+Framework+Dynamic+LINQ&p2=use+dynamic+where+with+Datetime+with+only+one+string

Alchoder
  • 61
  • 5