我有一个 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;
}
然后我在单元测试中执行以下方法调用(我所有的 objectDaos 都继承 entityDao):
[Test]
public void getOneByValueOfProperty()
{
Accomplishment result = accomplishmentDao.getOneByValueOfProperty
("AccomplishmentType.Name", "Publication");
Assert.IsNotNull(result);
}
以上通行证(AccomplishmentType 与成绩有关系)
Accomplishment result = accomplishmentDao.getOneByValueOfProperty("Description", "Can you hear me now?");
Accomplishment result = accomplishmentDao.getOneByValueOfProperty("LocalId", 4);
上述两项工作。然而,
Accomplishment result = accomplishmentDao.getOneByValueOfProperty
("Id", New Guid("95457751-97d9-44b5-8f80-59fc2d170a4c"));
不起作用并说以下内容:
Operator '=' incompatible with operand types 'Guid' and 'Guid
为什么会这样?Guid的不能比较?我也试过==
,但同样的错误。更令人困惑的是,我看到的每个 Dynamic Linq 示例都只是使用字符串,无论是使用参数化的 where 谓词还是我已经注释掉的这个:
//.Where(getProperty & "==" & CStr(getValue))
无论有没有 Cstr,许多数据类型都不适用于这种格式。我也尝试将 getValue 设置为字符串而不是对象,但随后我得到了不同的错误(例如多字字符串会在第一个字之后停止比较)。
使用 GUID 和/或任何数据类型进行这项工作时,我缺少什么?理想情况下,我希望能够只为 getValue 传递一个字符串(正如我在其他所有动态 LINQ 示例中看到的那样)而不是对象,并且无论列的数据类型如何,它都可以工作。