c# - IEnumerable<dynamic> with linq -
i trying build dynamic data context, linq not support dynamic type found solution on
http://jrwren.wrenfam.com/blog/2010/03/04/linq-abuse-with-the-c-4-dynamic-type/
public static class objectextensionmethod { public static ienumerable<dynamic> select(this object source, func<dynamic, dynamic> map) { foreach (dynamic item in source dynamic) { yield return map(item); } } public static ienumerable<dynamic> where(this object source, func<dynamic, dynamic> predicate) { foreach (dynamic item in source dynamic) { if (predicate(item)) yield return item; } } }
the problem solution getting data database after applying statement. there way apply statement before getting data database dynamic type
the problem solution getting data database after applying statement.
the problem here not dynamic, way iterating on source. using foreach
, , expect translated sql or sort of, wrong assumption. iterator created getenumerator()
method call query "materialized", if real type of source
implementing iqueryable<t>
, end else performed in memory.
if want conditions translated sql need to implement iqueryableprovider.
or, @ least can try call underlying iqueryableprovider
. i'm not sure if work.
public static class objectextensionmethod { public static iqueryable select<t>(this iqueryable source, expression<func<dynamic, dynamic>> map) { var method = new func<iqueryable<dynamic>, expression<func<dynamic, dynamic>>, iqueryable<dynamic>>(queryable.select).method; var call = expression.call(null, method, source.expression, expression.quote(map)); return source.provider.createquery(call); } public static iqueryable where(this iqueryable source, expression<func<dynamic, bool>> predicate) { var method = new func<iqueryable<dynamic>, expression<func<dynamic, bool>>, iqueryable<dynamic>>(queryable.where).method; var call = expression.call(null, method, source.expression, expression.quote(predicate)); return source.provider.createquery(call); } }
please note type of source
parameter has changed object
iqueryable
, types of map
, predicate
parameters have changed expression<func<,>>
.
Comments
Post a Comment