Can you pass a LINQ table to a function? -
i want send linq table (entity) parameter function value in select{}. purpose return value based on several properties of table/entity, , don't want of logic determines return value in linq statement. basically, line 'value = getvalue(tbl)' possible?
public class myclass { public int value { get; set; } } //some general function determine return value multiple properties private int getvalue(myentity cls) { if (cls.var1 == 1) return var2; if (cls.var1 == 2) return cls.var2 - cls.var3; return -1; } public list<myclass> getstuff(int itype) { list<myclass> myclass = (from tbl in context.myentity tbl.mytype == itype select new myclass { value = getvalue(tbl) }).tolist<myclass>(); return myclass; }
linq entities designed convert entire linq query equivalent sql query runs on server should use functions , expressions knows how translate sql. can't call arbitrary functions.
-this solved either rewriting calculation logic inside linq query , thing mention don't want.
-another solution add calculated properties myclass rely on data properties (var1, var2, var3,...) calculated.
-you use technique mentioned in blog post: http://daniel.wertheim.se/2011/02/07/c-clean-up-your-linq-queries-and-lambda-expressions/ put logic in central location outside linq query.
Comments
Post a Comment