Monday, 15 March 2010

ef code first - Call custom function in EF LINQ query Where clause -



ef code first - Call custom function in EF LINQ query Where clause -

env: ef6 + code first

i want able phone call custom function in clause of linq query

so line:

var activestaff = repo.staff.where(s => s.enddate == null || s.enddate.value > datetime.today);

becomes:

var activestaff = repo.staff.where(s => myedmfunctions.iscurrentstaff(s));

this have tried,

public class mycontext : dbcontext { protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.add(new mycustomconvention()); } } public class mycustomconvention : iconceptualmodelconvention<edmmodel> { public void apply(edmmodel item, dbmodel model) { var booltype = primitivetype.getedmprimitivetype(primitivetypekind.boolean); var stafftype = item.entitytypes.single(e => e.name == "staff"); var payload = new edmfunctionpayload { parametertypesemantics = parametertypesemantics.allowimplicitconversion, iscomposable = true, isniladic = false, isbuiltin = false, isaggregate = false, isfromprovidermanifest = true, parameters = new[] { functionparameter.create("staff", stafftype, parametermode.in) }, returnparameters = new[] { functionparameter.create("returntype", booltype, parametermode.returnvalue) } }; var function = edmfunction.create("iscurrentstaff", "my.core.data", dataspace.cspace, payload, null); item.additem(function); } } public static class myedmfunctions { [dbfunction("my.core.data", "iscurrentstaff")] public static bool iscurrentstaff(staff s) { homecoming s.enddate == null || s.enddate > datetime.today; } }

but i'm getting "specified method not supported." error entityframework's internal ctreegenerator class (after decompilation)

public override dbexpression visit(newrecordop op, node n) { throw new notsupportedexception(); }

can please confirm if there no way phone call custom function in clause?

i know it's possible create stored procedure , map in model. there way write in c#?

thanks.

just reply own question:

i'm going follow thread create andalso look solve problem.

extension method in clause in linq entities

linq ef-code-first code-first entity-framework-6

No comments:

Post a Comment