user2525910
user2525910

Reputation: 38

If condition in linq

I have a linq query which i cant get working fully. The query selects from a sql server view the user is able to select a filter(on screen) from one of four radio buttons(mutually exclusive using radio list ) which are Active,New,Obsolete,Any. So it is working if i select any of Active,New,Obsolete as this value is used in query(RBstatus.SelectedValue) but i cant get linq to say if 'Any' is selected miss the filter and just output every thing. The complete query is

    var query = (from b in context.mpcViewFormulas.Where(X => 
                    X.FormulaCode.Contains(formualcode)
                    && X.BaseCode.Contains(txtBase.Text)
                    && X.Status.Contains(RBstatus.SelectedValue))    

what i want to do is if 'Any' radio button is selected then miss the last && off the query (&& X.Status.Contains(RBstatus.SelectedValue) or is this the wrong way to go?

What i have tried is

   var query = (from b in context.mpcViewFormulas.Where(X => 
                   X.FormulaCode.Contains(formualcode)
                   && X.BaseCode.Contains(txtBase.Text)
                   if(RBstatus.SelectedValue !="Any")
                   {
                       && X.Status.Contains(RBstatus.SelectedValue)
                   })

this throws error query body must end with a select cluse or a group clause and invalid expression trem &&

Upvotes: 1

Views: 2312

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239220

You just need to add it as part of the existing conditional. You can't add an if statement in the middle. In other words, something like:

X.FormulaCode.Contains(formualcode) && 
X.BaseCode.Contains(txtBase.Text) &&
(RBstatus.SelectedValue == "Any" || X.Status.Contains(RBstatus.SelectedValue))

Alternatively, since these are all logical ANDs, you can simply chain the Where clauses, i.e.:

var query = (from b in context.mpcViewFormulas.Where(X => X.FormulaCode.Contains(formualcode)
                                             && X.BaseCode.Contains(txtBase.Text));
if (RBstatus.SelectedValue != "Any")
{
    query = query.Where(X => X.Status.Contains(RBstatus.SelectedValue));
}

As long as you don't evaluate the query by doing something like iterating over it or casting it to a list before calling Where again, you'll still have only one query issued.

UPDATE

If "Any" is not actually a value, then you essentially just have to exclude or include each possible selected value. For example, in the first method above, the conditional would need to change from RBstatus.SelectedValue == "Any" to:

(RBstatus.SelectedValue != "Active" && RBstatus.SelectedValue != "New" && RBstatus.SelectedValue != "Obsolete")

You could also be a little less verbose by utilizing a string array:

!(new string[] { "Active", "New", "Obsolete" }).Contains(RBstatus.SelectedValue)

It's debatable whether that's actually not as readable, though.

In the second method, since originally the conditional is RBstatus.SelectedValue != "Any", you'd need to do the opposite of what I just outlined. In other words:

(RBstatus.SelectedValue == "Active" || RBstatus.SelectedValue == "New" || RBstatus.SelectedValue == "Obsolete")

Or

(new string[] { "Active", "New", "Obsolete" }).Contains(RBstatus.SelectedValue)

(notice the ! is removed)

Upvotes: 6

Dario Griffo
Dario Griffo

Reputation: 4274

Guys up there gave you one answer, another option is remember that you can chain conditions until they are executed, so something like this is valid too:

var query = (from b in context.mpcViewFormulas.Where(X => X.FormulaCode.Contains(formualcode) && X.BaseCode.Contains(txtBase.Text));
if(RBstatus.SelectedValue !="Any")
{
    query = query.Where(X => X.Status.Contains(RBstatus.SelectedValue));
}

Upvotes: 1

Related Questions