EF6 IQueryable dynamic linq Where(predicate, values) with DbFunctions.TruncateTime

dynamic-linq entity-framework-6 sql-server


In my simplified example i have an object with following properties:

  • Name (string)
  • BirthDateTimeStamp (datetime)

I need to ba able to build dynamic queries in the following way

var predicate = "Name = @0";
var values = new object[]{"Ed"};
myIQueryableDataSource.Where(predicate, values)

This work good. Now i want to compare my datetime

var predicate = "BirthDateTimeStamp >= @0";
var values = new object[]{someDateTime};

This works good also. But what i actually want to do when comparing the datetimes and this issue shows itself better when doing the equals is comparing just on date.

var predicate = "BirthDateTimeStamp.Date >= @0";

This is not possible since the Date property is not recognized by EF to SQL server

var predicate = "System.Data.Entity.DbFunctions.TruncateTime(BirthDateTimeStamp) >= @0";

This is also not working since i can only access my object properties in the predicate.

How can i solve this in this way so the predicate stays in the string format. This code is just a part of a big existing parser for my queries and connat be completely rewritten.

1/13/2016 9:10:25 AM

Popular Answer

Answer given by @RockResolve did work, but is kind a hack. Linq Dynamics provide functionality to add custom functions

public class CustomTypeProvider: IDynamicLinkCustomTypeProvider
    public HashSet<Type> GetCustomTypes()
      HashSet<Type> types = new HashSet<Type>();
      // adding custom types
      return types;

// use below line to add this to linq
System.Linq.Dynamics.GlobalConfig.CustomTypeProvider = new CustomTypeProvier();
5/16/2018 6:58:29 AM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow