c# - Possible to wrap Expression<> and Func<> after Func defined? -
i create 2 kinds of identical mapping functions in view models map pocos, 1 queryables , 1 collections. possible create 1 method both can remove duplicate code? i'd keep expression<func<>>
use on local collections , entity framework.
public class myviewmodel { public static readonly expression<func<mypoco, myviewmodel>> asmap = (e) => new importpattern { id = e.id, name = e.name } public static readonly func<mypoco, myviewmodel>> tomap = (e) => new importpattern { id = e.id, name = e.name } public int id { get; set; } public string name { get; set; } }
examples: asmap succeeds entity framework project fields listed in mapping function.
var = product .where(p => p.id > 0 && p.id < 10) .orderby(q => q.id) .select( importpattern.asmap );
tomap works on local collections.
var products = products.where( p => p.id > 0 && p.id < 10).tolist(); var = products .orderby(q => q.id) .select( importpattern.tomap );
using asmap on local collections fails non-invocable member
error. here's few more fail:
.select( importpattern.tomap.compile().invoke() ) .select( o => importpattern.tomap.compile().invoke(o) )
fyi, not looking automapper() answer please.
what about
public static readonly expression<func<mypoco, myviewmodel>> asmap = (e) => new importpattern { id = e.id, name = e.name } public static readonly func<mypoco, myviewmodel>> tomap = asmap.compile();
it not hurt since once, rid of duplicate code.
Comments
Post a Comment