动态 Linq 帮助,不同的错误取决于作为参数传递的对象?
我有一个entityDao,它被我的每个objectDaos所继承。我正在使用 Dynamic Linq 并尝试让一些通用查询发挥作用。
通用方法中有以下代码:
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;
}
我的 EntityDao的 然后我在单元测试中执行以下方法调用(我所有的objectDaos都继承entityDao):
[Test]
public void getOneByValueOfProperty()
{
Accomplishment result = accomplishmentDao.getOneByValueOfProperty
("AccomplishmentType.Name", "Publication");
Assert.IsNotNull(result);
}
上面的pass(AccomplishmentType与completion有关系)
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
为什么会发生这种情况?指南不能比较吗?我也尝试过 ==
但同样的错误。更令人困惑的是,我见过的 Dynamic Linq 的每个示例都只是使用字符串,无论是使用参数化的 where 谓词还是我已经注释掉的这个:
//.Where(getProperty & "==" & CStr(getValue))
无论有或没有 Cstr,许多数据类型都不能使用这种格式。我尝试将 getValue 设置为字符串而不是对象,但随后我得到了不同的错误(例如多字字符串会在第一个单词之后停止比较)。
为了使其能够与 GUID 和/或任何数据类型一起使用,我缺少什么?理想情况下,我希望能够只为 getValue 传递一个字符串(正如我在每个其他动态 LINQ 示例中看到的那样)而不是对象,并让它工作,而不管列的数据类型如何。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我发现了这一点,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 接口替换为以下内容:
现在我的 getOneByValueOfProperty 始终有效!
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:
Now my getOneByValueOfProperty works all the time!
对于Guid,这应该有效:
(注意参数getValue应该是Guid类型)
for Guid this should work:
(note the parameter getValue should be of type Guid)