linq - order by lambda expression with expression trees in C# -
my requirement order table according column in runtime. went through path of parameter expression. able achieve when columns present in single table. here piece of code:
my requirement order table according buildstatusorder
during run time:
list<buildstatusenum> buildstatusorder = new list<buildstatusenum> { buildstatusenum.h, buildstatusenum.f, buildstatusenum.a }; parameterexpression parameterexpression = expression.parameter(typeof(companyqueue)); memberexpression memberexpression = expression.propertyorfield(parameterexpression, "buildstatusid"); memberexpression valueexpression = expression.property(memberexpression, "value"); expression orderbyexpression = expression.constant(buildstatusorder.count); (int statusindex = buildstatusorder.count - 1; statusindex >= 0; statusindex--) { constantexpression constantexpression = expression.constant((int)buildstatusorder[statusindex]); constantexpression indexconstantexpression = expression.constant(statusindex); binaryexpression equalexpression = expression.equal(valueexpression, constantexpression); orderbyexpression = expression.condition(equalexpression, indexconstantexpression, orderbyexpression); } memberexpression hasvalueexpression = expression.property(memberexpression, "hasvalue"); conditionalexpression nullcheckexpression = expression.condition(hasvalueexpression, orderbyexpression, expression.constant(buildstatusorder.count)); var orderbylambda = expression.lambda<func<companyqueue, int>>(nullcheckexpression, parameterexpression); using (kapowcontext context = new kapowcontext()) { var queue = context.companyqueues .where(cq => cq.u.s.cq.userid == utilities.authentication.userid) .where(cq => buildstatusorder.contains((buildstatusenum)cq.u.s.cq.buildstatusid)) .orderby(o => o.u.se.segmentid) .thenby(orderbylambda)
this works fine. have display additional column. table. joined tables. lambda expression looks this:
var queue = context.companyqueues .join(context.abcd, cq => cq.robotid, r => r.robotid, (cq, r) => new { cq, r }) .join(context.efgh, s => s.r.segmentid, se => se.segmentid, (s, se) => new { s, se }) .join(context.hijk, u => u.s.cq.userid, => us.userid, (u, us) => new { u, }) .where(cq => cq.u.s.cq.userid == utilities.authentication.userid) .where(cq => buildstatusorder.contains((buildstatusenum)cq.u.s.cq.buildstatusid)) .orderby(orderbylambda)
now orderbylambda
not working. getting following error:
'
system.linq.queryable.thenby<tsource,tkey>(system.linq.iorderedqueryable<tsource>, system.linq.expressions.expression<system.func<tsource,tkey>>)
' cannot inferred usage. try specifying type arguments explicitly.
Comments
Post a Comment