Using DateDiff with Linq.Dynamic library for fetching today records

Use DbFunctions

.Where(p => DbFunctions.DiffDays(p.AddedDate, DateTime.Now) == 0)

Edit:

If you want to invoke this dynamically, you'll need to modify code for the Dynamic LINQ.

  1. Download the sample project containing DynamicLibrary.cs. The file is located under App_Code folder.
  2. Find the static definition for predefinedTypes and add typeof(DbFunctions) at the very end.

Now you will be able to do this:

.Where("DbFunctions.DiffDays(AddedDate, DateTime.Now) = 0")

And it will be translated to this SQL:

WHERE 0 = (DATEDIFF (day, [Extent1].[AddedDate], SysDateTime()))

flindeberg is correct when he say that System.Linq.Dynamic parses the expression that you give as C#, not SQL.

However, Entity Framework defines the class "DbFunctions" that allows you call sql functions as part of your Linq queries.

DbFunctions.DiffDays is the method that you are looking for. With this, you also don't need to be using System.Linq.Dynamic.

Your code would look something like this, I think:

     var _list = new vsk_error_log();
     using ( var entities = new vskdbEntities() )
     {
        _list = entities.vsk_error_log
          .Where( entry => DbFunctions.DiffDays( entry.added_date, DateTime.UtcNow ) == 0 )
          .ToList();
     }
     return _list;

If you want to use this function with System.Linq.Dynamic, it would look something like this:

     var _list = new vsk_error_log();
     using ( var entities = new vskdbEntities() )
     {
        _list = entities.vsk_error_log
          .Where( "DbFunctions.DiffDays( added_date, DateTime.UtcNow ) == 0" )
          .ToList();
     }
     return _list;

HOWEVER! System.Linq.Dynamic will not recognize the class DbFunctions, and as such, this will not work out of the box. However, we can "patch" this functionality in using a bit of reflection, although it can be a bit ugly:

     var type = typeof( DynamicQueryable ).Assembly.GetType( "System.Linq.Dynamic.ExpressionParser" );

     FieldInfo field = type.GetField( "predefinedTypes", BindingFlags.Static | BindingFlags.NonPublic );

     Type[] predefinedTypes = (Type[])field.GetValue( null );

     Array.Resize( ref predefinedTypes, predefinedTypes.Length + 1 );
     predefinedTypes[ predefinedTypes.Length - 1 ] = typeof( DbFunctions );

     field.SetValue( null, predefinedTypes );

By running this code, System.Linq.Dynamic will now recognize DbFunctions as a type that can be used in the parsed C# expressions.