I'm using FluentData as an orm for my database and I'm trying to create a generic query method:
internal static T QueryObject<T>(string sql, object[] param, Func<dynamic, T> mapper)
{
return MyDb.Sql(sql, param).QueryNoAutoMap<T>(mapper).FirstOrDefault();
}
Except in my class's function:
public class MyDbObject
{
public int Id { get; set; }
}
public static MyDbObject mapper(dynamic row)
{
return new MyDbObject {
Id = row.Id
};
}
public static MyDbObject GetDbObjectFromTable(int id)
{
string sql = @"SELECT Id FROM MyTable WHERE Id=@Id";
dynamic param = new {Id = id};
return Query<MyDbObject>(sql, param, mapper);
}
at Query<MyDbObject>(sql, param, mapper)
the compilers says:
An anonymous function or method group connot be used as a constituent value of a dynamically bound object.
Anyone have an idea of what this means?
Edit:
The compiler doesn't complain when I convert the method into a delegate:
public static Func<dynamic, MyDbObject> TableToMyDbObject =
(row) => new MyDbObject
{
Id = row.Id
}
It still begs the question of why one way is valid but not the other.
The compiler doesn't complain when I convert the method into a delegate:
The issue is exactly as the error says...
It simply means that you can't use an anonymous function because one of the parameters is Type dynamic, so to fix your method you could simply cast the param to
object
Or presumably from looking at your code...simply.
The reason it doesn't complain when you use the Func delegate is because you never invoke the DLR by using the type dynamic so there is no dynamically bound operation.