arun thatham
arun thatham

Reputation: 500

Unable to compare DateTime in Dynamic Linq

I'm using dynamic linq to build a query in runtime. I'm preparing the following command

var temp = list
    .AsQueryable()
    .Where("Date<DateTime(2014,10,22,21,05,56)")
    .Select("it");
return temp.Cast<T>().ToList();

The condition is not executing properly and i'm getting a wrong set of results. My filter condition is dynamic and i'll generate it only in run time. Can you help me in resolving this issue. Is there a Hour minute second comparison possible in dynamic linq?

Upvotes: 0

Views: 1873

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109117

When I use the NuGet package I can execute your predicate alright. But I would use a parameter anyway:

var temp = list
    .AsQueryable()
    .Where("Date < @0", new DateTime(2014,10,22,21,05,56))
    .Select("it");
return temp.Cast<T>().ToList();

Upvotes: 1

Rama Kathare
Rama Kathare

Reputation: 930

Please refer this link

As per the information from that link

Note that constructor invocations are not prefixed by new. The following example creates a DateTime instance for a specfic year, month, and day using a constructor invocation:

orders.Where("OrderDate >= DateTime(2007, 1, 1)");

Only difference I can see there is it is taking only 3 arguments. The author may not have implemented the overloaded version. Try with three parameters, instead.

Credits

Upvotes: 2

Related Questions