Question

I'm using the dynamic LINQ library by Scott Guthrie together with Entity Framework and C#.

I have to build my where string into a variable based on several factors and then pass the string variable to the where clause. For some reason, this will work:

ContactList = ContactList.Where("DateAdded >= @0", DateTime.Parse("12/1/2012"));

But this will not work

string WhereClause = string.Format("DateAdded >= {0}", DateTime.Parse("12/1/2012"));
ContactList = ContactList.Where(WhereClause);

As mentioned, I need to use it in the version of passing the variable. Anyone know why the second doesn't work?

Thanks in advance!

Was it helpful?

Solution 2

It seems what I was trying to do is not possible with the current DynamicLINQ library. The reason it didn't work was well outlined below by Tilak.

My solution was to modify the DynamicLINQ library to allow the query to be written as a string and passed to the where clause for Date/Time datatypes. The modification was found here by Paul Hatcher: LINQ TO SQL, Dynamic query with DATE type fields

OTHER TIPS

I was able to get it working with a slightly different string format using the information here.

Doing this worked fine for me:

ContactList.Where("DateAdded >= DateTime(2013, 06, 18)")

Note this does not work at all with DateTimeOffset columns.

ObjectQuery.Where overload accepts 2 parameters.

  1. string predicate
  2. params ObjectParameter[] parameters

In your first example, Where builds the query (where clause) using ObjectParameter parameters (using Name, Type and Value of ObjectParameter)

In your second example, whatever is passed is treated as final where clause (no internal conversion based on datatype of passed parameters done).

Based on Richard Rout's option, with a slight modification:

ContactList.Where("DateAdded >= DateTime(2013, 06, 18)")

This works in my Linq2Entities solution. Note the DateTime instead of Date. Hope this saves someone the headache this problem gave me.

ContactList.Where probably puts quotes around non-numeric arguments (such as a DateTime). In your second string the date isn't quoted.

Had similar issue with dynamic linq core and ef 3.1 with sql server, solved it by explicitly setting up column type:

  1. Fluent way:

    modelBuilder.Entity<Contact>().Property(p => p.DateAdded).HasColumnType("datetime")
    
  2. Attribute way:

    public class Contact
    {
         [Column(TypeName = "datetime")]
         public DateTime DateAdded { get; set; }
    }
    

Usage:

ContactList = ContactList.Where("DateAdded == @0", "2021-03-25 02:29:00.000");

sql server datetime format: YYYY-MM-DD hh:mm:ss[.nnn]

I know this is an old question but if someone is still encountering this issue with ASP.Net Core, I was able to resolve it as follows.

Basically you have to add the Entity class(table column) with the Column attribute and specifying the TypeName. For the above question, it's as follows:

[Column(TypeName = "datetime")]
public DateTime DateAdded { get; set; }

That should fix the datatype conversion errors.

Source : https://github.com/zzzprojects/System.Linq.Dynamic.Core/issues/240

Kind of an old question, but in case this is helpful to anyone else, my issue was that it was taking my DateTime and making it a string. So the simple fix was to make it a date again. Something like:

var where = $"{propertyName} >= DateTime.Parse(\"{startDate}\") and {propertyName} <= DateTime.Parse(\"{endDate}\")";
query = query.Where(where);

It's a little more typing than adding the column attribute as The_Outsider said, but just another option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top