动态Linq帮助,根据作为参数传递的对象的不同错误?

Sve*_*ang 5 linq generics dynamic-linq where-clause linq-to-sql

我有一个entityDao,由我的objectDaos的每个人继承.我正在使用Dynamic Linq并尝试使用一些通用查询.

我的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;
}
Run Code Online (Sandbox Code Playgroud)

        然后我在单元测试中执行以下方法调用(我的所有objectDaos都继承entityDao):

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

    Assert.IsNotNull(result);
}
Run Code Online (Sandbox Code Playgroud)

以上过程(AccomplishmentType与成就有关系)

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

以上两项工作.然而,

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

不起作用并说以下内容:

Operator '=' incompatible with operand types 'Guid' and 'Guid
Run Code Online (Sandbox Code Playgroud)

为什么会这样?Guid的无法比较?我试过==但同样的错误.甚至更令人困惑的是,动态Linq的每个例子我都看到使用字符串无论是使用参数化的谓词还是我已经注释掉的这个:

//.Where(getProperty & "==" & CStr(getValue))
Run Code Online (Sandbox Code Playgroud)

无论有没有Cstr,许多数据类型都不适用于此格式.我尝试将getValue设置为字符串而不是对象,但是我只是得到不同的错误(例如多字符串会在第一个单词后停止比较).

使用GUID和/或任何数据类型时,我缺少什么?理想情况下,我希望能够为getValue传递一个字符串(正如我在其他所有动态LINQ示例中看到的那样)而不是对象,并且无论列的数据类型如何都能使其工作.

Sve*_*ang 6

Welp我想到了这一点,Dynamic LINQ最初并不支持GUID的比较(这么愚蠢!).我找到了这个小小的问题:https://connect.microsoft.com/VisualStudio/feedback/details/333262/system-linq-dynamic-throws-an-error-when-using-guid-equality-in-where-clause

您只需编辑Dynamics.cs,将IEqualitySignatures接口替换为以下内容:

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);
}
Run Code Online (Sandbox Code Playgroud)

现在我的getOneByValueOfProperty一直在工作!