为什么在尝试在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();
希望这有助于其他人。
答案 0 :(得分:2)
您将args传递给匿名对象,而不是对象数组(params)。因此它成为第一个参数,并且无法绑定,因为您的查询需要2个参数。此外,Query
会返回IEnumerable<dynamic>
类型。
将您的代码更改为:
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});