2

I am using dynamic linq on a project but don’t know how to run a particular query based on parameters. The example below shows the dynamic linq used to run 3 separate queries where a user has entered a filter expression or filer and sort expression etc. Without using if statements to check I (which seems incorrect and messy as I could end up with lots of if statements to cover the many different permutations ) I am unsure of how to generate the statements dynamically. Is this possible? I am running this query against Entity Framework.

context.Users.AsQueryable().Where(filterExpression)
context.Users.AsQueryable().Where(filterExpression).OrderBy(sortExpression)
context.Users.AsQueryable()Take(10).Where(filterExpression).OrderBy(sortExpression)
2
  • Try the dynamic linq library weblogs.asp.net/scottgu/archive/2008/01/07/… Commented May 25, 2012 at 8:01
  • That's what I'm using but need to build up the dynamic linq expression 'Dynamically' based on user input. Commented May 25, 2012 at 8:30

1 Answer 1

3

That should be achieved using expression tree only or may be using if..else clause

How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic)

Sign up to request clarification or add additional context in comments.

2 Comments

The if..else clause feels wrong to me hence the question. The clauses could possible grow very large depending on the parameters passed. However if this and expression trees is the only way to go then I may investigate expression trees a little further.
I decided to go with the if ..else route as expression trees with dynamic linq seemed overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.