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

发布于 2024-08-28 22:27:21 字数 2078 浏览 11 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-09-04 22:27:21

好吧,我发现了这一点,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);
}

现在我的 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:

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!

花落人断肠 2024-09-04 22:27:21

对于Guid,这应该有效:

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

(注意参数getValue应该是Guid类型)

for Guid this should work:

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

(note the parameter getValue should be of type Guid)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文