orm - C# - Passing an anonymous function as a parameter -
i'm using fluentdata orm database , i'm trying create 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 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 mytable id=@id"; dynamic param = new {id = id}; return query<mydbobject>(sql, param, mapper); } at query<mydbobject>(sql, param, mapper) compilers says:
an anonymous function or method group connot used constituent value of dynamically bound object.
anyone have idea of means?
edit:
the compiler doesn't complain when convert method delegate:
public static func<dynamic, mydbobject> tabletomydbobject = (row) => new mydbobject { id = row.id } it still begs question of why 1 way valid not other.
the issue error says...
an anonymous function or method group cannot used constituent value of dynamically bound operation.
it means can't use anonymous function because 1 of parameters type dynamic, fix method cast param object
public static mydbobject getdbobjectfromtable(int id) { string sql = @"select id mytable id=@id"; dynamic param = new {id = id}; // type dynamic causes issue. // fix cast object return query<mydbobject>(sql, (object)param, mapper); } or presumably looking @ code...simply.
return query<mydbobject>(sql, id, mapper); the reason doesn't complain when use func delegate because never invoke dlr using type dynamic there no dynamically bound operation.
Comments
Post a Comment