Different failures based on the object supplied as a parameter when using Dynamic Linq?

dynamic-linq generics linq linq-to-sql where-clause


I have an entityDao that is inherbited by everyone of my objectDaos. I am using Dynamic Linq and trying to get some generic queries to work.

I have the following code in my generic method in my EntityDao :

public abstract class EntityDao<ImplementationType> where ImplementationType : Entity
    public ImplementationType getOneByValueOfProperty(string getProperty, object getValue){
    ImplementationType entity = null;
    if (getProperty != null && getValue != null) 
        LCFDataContext lcfdatacontext = new LCFDataContext(); 
         //Generic LINQ Query Here
         entity = lcfdatacontext.GetTable<ImplementationType>().Where(getProperty + " =@0", getValue).FirstOrDefault();
         //.Where(getProperty & "==" & CStr(getValue))


return entity;

        Then I do the following method call in a unit test (all my objectDaos inherit entityDao):

public void getOneByValueOfProperty()
    Accomplishment result = accomplishmentDao.getOneByValueOfProperty
        ("AccomplishmentType.Name", "Publication");


The above passes (AccomplishmentType has a relationship to accomplishment)

Accomplishment result = accomplishmentDao.getOneByValueOfProperty("Description", "Can you hear me now?");
Accomplishment result = accomplishmentDao.getOneByValueOfProperty("LocalId", 4);

Both of the above work. However,

 Accomplishment result = accomplishmentDao.getOneByValueOfProperty
    ("Id", New Guid("95457751-97d9-44b5-8f80-59fc2d170a4c"));

Does not work and says the following:

Operator '=' incompatible with operand types 'Guid' and 'Guid

Why is this happening? Guid's can't be compared? I tried == as well but same error. What's even moreso confusing is that every example of Dynamic Linq I have seen simply usings strings whether using the parameterized where predicate or this one I have commented out:

//.Where(getProperty & "==" & CStr(getValue))

With or without the Cstr, many datatypes don't work with this format. I tried setting the getValue to a string instead of an object as well, but then I just get different errors (such as a multiword string would stop comparison after the first word).

What am I missing to make this work with GUIDs and/or any data type? Ideally I would like to be able to just pass in a string for getValue (as I have seen for every other dynamic LINQ example) instead of the object and have it work regardless of the data Type of the column.

4/8/2010 9:18:08 PM

Accepted Answer

Welp I figured this out, Dynamic LINQ doesn't initially support Comparison of GUIDs (so stupid!). I found this little tidbit: https://connect.microsoft.com/VisualStudio/feedback/details/333262/system-linq-dynamic-throws-an-error-when-using-guid-equality-in-where-clause

You just go in an edit Dynamics.cs, replace the IEqualitySignatures interface with the following:

interface IEqualitySignatures : IRelationalSignatures
    void F(bool x, bool y);
    void F(bool? x, bool? y);
    void F(Guid x, Guid y);
    void F(Guid? x, Guid? y);

Now my getOneByValueOfProperty works all the time!

4/9/2010 6:16:32 PM

Popular Answer

for Guid this should work:

.Where(getProperty + ".Equals(@0)", getValue);

(note the parameter getValue should be of type Guid)

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