大规模查询时出现 RuntimeBinderException
为什么在尝试在 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.
您正在匿名对象中传递参数,而不是对象数组(params)。因此,它成为第一个参数,并且无法绑定,因为您的查询需要 2 个参数。此外,
Query
返回类型IEnumerable
。将您的代码更改为:
或者您可以使用显式对象数组:
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 typeIEnumerable<dynamic>
.Change your code to this:
Or you could use explicit object array: