Hello again geniuses,

I want to be able to execute dynamic queries with DO.

For example, on a search form, I want the user to be able to pick the type of entity he wants to search. Once he picks the type of entity, I will display a list of the entity's persistent properties. For each persistent property, the user can select among various filter operators (e.g. EQUALS, NOT EQUALS, LESS THAN, GREATER THAN, etc.), and then specify the criteria for that filter.

For example, the user can pick the "Customer" entity, and I will display the Name and CompanyName properties available for search. For CompanyName, he can select "EQUALS" and enter "Xtensive" as the filter.

I would then like to execute Query.All<customer>().Where(customer => customer.CompanyName == "Xtensive")

Would you happen to know how I can do that?

Maybe I can do typeof(Query).GetMethod("All").MakeGenericMethod(entityType).GetMethod("Where").MakeGenericMethod(...).Invoke(...)

But I would need to pass an expression of type Func<customer, bool=""> to the second call to MakeGenericMethod() and also the actual delegate to Invoke()

Is there an easier way? If not, do you have any suggestions for building the expression and delegate?

The actual delegate would need to be customer => customer.CompanyName == "Xtensive", or it could be entity => entity[propertyName] == value

Thank you!


Updated at 16.07.2010 9:30:48

Using Dynamic LINQ library...

http://weblogs.asp.net/scottgu/archive/ ... brary.aspx

var customer = new Customer() { Name = "Alex" };

            var entityType = typeof (Customer);
            var queryType = typeof(Query);
            var methodInfo = queryType.GetMethod("All");
            var method = methodInfo.MakeGenericMethod(entityType);
            var queryable = (IQueryable)method.Invoke(null, null);

            var results = DynamicQueryable.Where(queryable, "Name==\"Alex\"");

works like a charm.

Any better suggestions?

This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Jul 16 '10 at 08:56

ara's gravatar image

ara
395878791


One Answer:

Likely, no.

The idea to develop a general purpose control allowing to visually build such LINQ queries over any LINQ provider lives in my head for pretty long time :) The task is pretty common, but it seems there is still no simple solution.

answered Jul 17 '10 at 15:00

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA