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))
}
//lcfdatacontext.SubmitChanges()
//lcfdatacontext.Dispose()
return entity;
}
Then I do the following method call in a unit test (all my objectDaos inherit entityDao):
[Test]
public void getOneByValueOfProperty()
{
Accomplishment result = accomplishmentDao.getOneByValueOfProperty
("AccomplishmentType.Name", "Publication");
Assert.IsNotNull(result);
}
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With