1

I have two Guid collections:

List<Guid> statuses;
List<Guid> priorities;

How to make the following query:

var result = context.Activity.Where(a => 
    (a.StatusID == statuses[0] || a.StatusID == statuses[1] || ... || a.StatusID == statuses[n]) &&
    (a.PriorityID == priorities[0] || a.PriorityID == priorities[1] || ... || a.PriorityID  == priorities[m]))

Collections could be empty and in this case we shouldn't add an appropriate 'AND' condition. How to do such dynamic query?

UPDATE

Hmm, imagine I need something like this:

List<Func<Activity, bool>> conds = new List<Func<Activity, bool>>();

var result = context.Activity.Where(conds[0] || (conds[1] && conds[2]))

How to do that?

3 Answers 3

5

First answer based on question
Selects only those Activity objects of which the StatusID is in statusses and the PriorityID is in priorities. Obviously if the collections are empty none of the Activity objects will match the condition.

var result = context.Activity.Where(a => 
    statuses.Contains(a.StatusID) && priorities.Contains(a.PriorityID));

Alternative answer based on @Don Tomato's comment
There must have been a misunderstanding based on the way you formulated the question. However, I think what you want is to add conditions to an IQueryable as required.

List<Func<Activity, bool>> conditions = new List<Func<Activity, bool>>();
// add conditions to the list
// for eaxmple:
// conditions.add(a => statuses.Contains(a.StatusID));
// or 
// conditions.add(a => a.Name == "Don Tomato");

IQueryable query = context.Activity.AsQueryable();
foreach (Func<Activity, bool> condition in conditions)
   query = query.Where(condition);

List<Activity> result = query.ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Agreed, I missed the obvious dammit!
@DonTomato, This should send an SQL [StatusID] IN (<statusID>, ...) to your database server.
Good answer, thanks!, but this is not quite what I expected. Suppose I'm going to design a very complex query, where, depending on certain conditions, will add or remove different AND and OR conditions. I would like to see a way to construct a query like in Lego constructions. I need to build the filter expression step-by-step with an 'and/or' combination of each part, instead of inventing ingenious and elegant way to solve a simple problem.
0

You can chain where clauses together like this and the end result is that they will be anded together:

var query = context.Activity.Where(o => <condition1>);

if (2ndconditionrequired)
{
    query = query.where(o => <condition2>);
}

query.ToList();

Comments

0

You could also do it like this:

var result = context.Activity;

if (statuses != null && statuses.Count > 0)
{
    result = results.Where(a => 
        a.StatusID == statuses[0] || a.StatusID == statuses[1] || ... || a.StatusID == statuses[n];
}

if (priorities != null && priorities.Count > 0)
{
    result = results.Where(a => 
        a.PriorityID == priorities[0] || a.PriorityID == priorities[1] || ... || a.PriorityID  == priorities[m]);
}

result.ToList(); // The query won't be executed until here.

But I guess what you really want to do is as @Bazzz answered.

Comments

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.