c# - Dynamically add filter to Linq queryable which calls a DataContext method -
i having trouble creating expression add queryable filter condition. replicate following:
starting simple queryable:
iqueryable<customer> query = customer in context.customer select customer;
i dynamically add filter condition involves calling method on datacontext:
iqueryable<customer> query = customer in context.customer customer.name == context.convertvalueforsearch(searchstring) select customer;
so 'convertvalueforsearch' calls appropriate database function when evaluated.
the above produces following sql:
exec sp_executesql n'select [t0].[custname] [dbo].[customer] [t0] [t0].[custname] = [dbo].[fnconvertsearchstringvalue](@p0)',n'@p0 varchar(8000)',@p0='foo'
we have number of datacontext derived types, convertvalueforsearch defined through interface (isearchvalueconverter) each of our datacontext types implement.
i not sure how correctly create expression call convertvalueforsearch(string searchvalue) on datacontext , create final expression.equal made of property accessor customer.name , method call convertvalueforsearch filter can applied queryable createquery.
if start following:
... code executed in repository instance <func<manager<tentity, trootdal, tuniqueidentifiertype>, string, string>> convertsearchstringvaluelambda = (manager, searchvaluetoconvert) => manager.datacontextforconversionforspecificcontext.convertvalueforsearch(searchvaluetoconvert); var invokedcall = expression.invoke(convertsearchstringvaluelambda, expression.constant(this), expression.constant(searchvalue)); ...code executed in filter item instance apply filter queryable // accessor customer.name lambdaexpression propertyaccessor = entity<tdalentity>.getpropertyaccessorexpression<string>(this.filterfield); binaryexpression filterexpression = expression.equal(propertyaccessor.body, filtervalue); expression filter = expression.lambda(filterexpression, propertyaccessor.parameters); query = query.provider.createquery<tdalentity>( expression.call( typeof(queryable), "where", new type[] { query.elementtype }, query.expression, filter));
the call fnconvertsearchstringvalue done in separate call db , results of passed database when queryable used. although might ok in circumstances, need function called in sql generated linq-to-sql above.
thank suggestions.
Comments
Post a Comment