c# - MethodCallExpression is not setting up the orderby correctly -
i'm new expression business. modeling after example: https://msdn.microsoft.com/en-us/library/vstudio/bb882637(v=vs.110).aspx
i trying list of offices satisfies specific name. code works point need setup order by. keep getting error:
parameterexpression of type 'db.office' cannot used delegate parameter of type 'system.string'
here code
iqueryable<office> offices = getalloffices(); parameterexpression pe = expression.parameter(typeof(office), "office"); expression left = expression.property(pe, typeof(office).getproperty("officename")); expression right = expression.constant(filterrequest.filters.value); expression e1 = expression.equal(left, right); expression predicatebody = e1; methodcallexpression wherecallexpression = expression.call( typeof(queryable), "where", new type[] { offices.elementtype }, offices.expression, expression.lambda<func<office, bool>>(predicatebody, new parameterexpression[] { pe })); methodcallexpression orderbycallexpression = expression.call( typeof(queryable), "orderby", new type[] { offices.elementtype, offices.elementtype }, wherecallexpression, expression.lambda<func<string, string>>(pe, new parameterexpression[] { pe })); iqueryable<string> results = offices.provider.createquery<string>(orderbycallexpression);
your query returns office
, should
iqueryable<office> results = offices.provider.createquery<office>(orderbycallexpression);
note orderby
wrong...
it should like:
methodcallexpression orderbycallexpression = expression.call( typeof(queryable), "orderby", new type[] { offices.elementtype, typeof(string) }, wherecallexpression, expression.lambda<func<office, string>>(left, new parameterexpression[] { pe }));
if want order officename
. wrote was: .orderby(x => x)
quite useless, because rows of table don't have ordering. have rewritten .orderby(x => x.officename)
perhaps want add .select(x => x.officename)
after orderby
methodcallexpression selectcallexpression = expression.call( typeof(queryable), "select", new type[] { offices.elementtype, typeof(string) }, orderbycallexpression, expression.lambda<func<office, string>>(left, new parameterexpression[] { pe }));
then is:
iqueryable<string> results = offices.provider.createquery<string>(selectcallexpression);
Comments
Post a Comment