Performing dynamic expression with unknown types

If someone is very popular with the Linq.Dynamic namespace, I could use some help - I could not find any independent resources on the Internet.

I mainly use DynamicExpression.ParseLambda to create an expression when the type is not known at compile time,

public Expression GetExpression(Type t, List<QueryFilter> filters) { // pseudo code // extracts a string representation of the query as 'expressionString' return DynamicExpression.ParseLambda(t, typeof(Boolean), expressionString, values); } 

Where is the QueryFilter:

 public class QueryFilter { string propertyName; ExpressionType operationType; object value; } 

What is a simple binary function like "Age> 15" or something like that.

This is how the GetExpression function works, it takes two types: input type and output type, and ultimately generates what is usually created using the Func delegate. It also takes a string representing the query and the params [] object of the values, which are respectively the expressions and expressions of 'expressionString'.

However, I am unable to execute the dynamic expression in LINQ-to-SQL using the DataContext generated from the SqlMetal file (.dbmc).

 DatabaseContext db = new DatabaseContext(connectionString); var filter = DynamicExpressionBuilder. GetExpression(typeof(SysEventLogT), sysEventFilters) var query = db.SysEventLogT.Where(filter); 

It produces the following error:

 System.Data.Linq.Table<DBReporting.Linq.Data.SysEventLogT> 

no definition of "where" and best method of overloading

 System.Linq.Dynamic.DynamicQueryable.Where<T>(System.Linq.IQueryable<T>, string, params object[]) 

has some invalid arguments.

I know that my DataContext instance really treats sql tables as properties ... do I need to somehow think with GetProperty () for this to work? Or maybe I need to create something else. Where is the extension?

+6
c # lambda linq expression-trees dynamic-linq
source share
1 answer

Your GetExpression returns the type of the expression - the DynamicQueryable.Where method, when used as an extension method, expects the string to become the first parameter.

You need your call, where it will look like this:

 var query = db.SysEventLogT.Where("Age > @0", 15); 

Alternatively, you can try the following, just to be explicit:

 var query = db.SysEventLogT.AsQueryable().Where("Age > @0", 15); 

Note that if simpler, you can create a sting containing a full filter and not use the params object [] parameter:

 var query = db.SysEventLogT.AsQueryable().Where("Age > 15"); 
+2
source share

All Articles