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.
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});