大规模查询时出现 RuntimeBinderException

发布于 12-03 22:26 字数 857 浏览 0 评论 0原文

为什么在尝试在 Massive 中执行以下查询时会出现运行时绑定程序异常?

  public dynamic Find(string email, string type)
    {
        dynamic result = new ExpandoObject();
        result = this.Query(@"SELECT * FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new {email, type});
        return result;
    }

编辑以显示解决方案: 我需要更改查询以确保仅返回一列名为“Id”的列。我收到绑定错误,因为成员和地址中的多个列都有一个名为“Id”的列。为了在我的查询中获得单个结果,我必须将其修改为:

result = this.Query(@"SELECT a.* FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new object[] { email, type }).Single();

希望这对其他人有帮助。

Why am I getting a runtime binder exception when trying to execute the following query in Massive?

  public dynamic Find(string email, string type)
    {
        dynamic result = new ExpandoObject();
        result = this.Query(@"SELECT * FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new {email, type});
        return result;
    }

EDIT TO SHOW SOLUTION:
I needed to change my query to ensure only one column with the name 'Id' was returned. I was getting a binding error because multiple columns in Members and Addresses had a column named 'Id'. To get a single result in my query I had to modify it to this:

result = this.Query(@"SELECT a.* FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new object[] { email, type }).Single();

Hope this helps someone else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我不咬妳我踢妳2024-12-10 22:26:14

您正在匿名对象中传递参数,而不是对象数组(params)。因此,它成为第一个参数,并且无法绑定,因为您的查询需要 2 个参数。此外,Query 返回类型 IEnumerable

将您的代码更改为:

public dynamic Find(string email, string type)
{
    IEnumerable<dynamic> result;
    result = this.Query(@"SELECT a.* FROM Addresses a 
        INNER JOIN Members m ON m.Id = a.MemberId 
        INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
        WHERE m.Email = @0 AND at.Type = @1", email, type);

    // decide how you want to handle multiple results here
    return result.FirstOrDefault();
}

或者您可以使用显式对象数组:

        WHERE m.Email = @0 AND at.Type = @1", new object[] {email, type});

You are passing the args in an anonymous object, instead of an object array (params). It therefore becomes the first argument and fails to bind as your query expects 2 arguments. In addition Query returns type IEnumerable<dynamic>.

Change your code to this:

public dynamic Find(string email, string type)
{
    IEnumerable<dynamic> result;
    result = this.Query(@"SELECT a.* FROM Addresses a 
        INNER JOIN Members m ON m.Id = a.MemberId 
        INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
        WHERE m.Email = @0 AND at.Type = @1", email, type);

    // decide how you want to handle multiple results here
    return result.FirstOrDefault();
}

Or you could use explicit object array:

        WHERE m.Email = @0 AND at.Type = @1", new object[] {email, type});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文