tags:

views:

515

answers:

3

Looking up LINQ and Or in google is proving somewhat difficult so here I am.

I want to so the following:

(from creditCard in AvailableCreditCards 
where creditCard.BillToName.ToLowerInvariant().Contains(txtFilter.Text.ToLowerInvariant())
**or creditCard.CardNumber.().Contains(txtFilter.Text)**
orderby creditCard.BillToName
select creditCard)
+2  A: 

You can use the .Where() function to accomplish this.

var cards = AvailableCreditCards.Where(card=> card.CardNumber.Contains(txtFilter.Text) || card.BillToName.ToLowerInvariant().Contains(txtFilter.Text.ToLowerInvariant());
Jason Jackson
+7  A: 

C# keywords supporting LINQ are still C#. Consider where as a conditional like if; you perform logical operations in the same way. In this case, a logical-OR, you use ||

(from creditCard in AvailableCreditCards 
 where creditCard.BillToName.ToLowerInvariant().Contains(
             txtFilter.Text.ToLowerInvariant())  
 || creditCard.CardNumber.().Contains(txtFilter.Text) 
orderby creditCard.BillToName 
select creditCard)
James Curran
+1  A: 

You should use the boolean operator that works in your language. Pipe | works in C#

Here's msdn on the single | Here's msdn on the double pipe ||

The deal with the double pipe is that it short-circuits (does not run the right expression if the left expression is true). Many people just use double pipe only because they never learned single pipe.

Double pipe's short circuiting behavior creates branching code, which could be very bad if a side effect was expected from running the expression on the right.

The reason I prefer single pipes in linq queries is that - if the query is someday used in linq to sql, the underlying translation of both of c#'s OR operators is to sql's OR, which operates like the single pipe.

David B
That would be two pipes, please.
DOK
i believe that one pipe is a bitwise operator and two pipes is a logical or operator.
David Alpert
Did not know that! Very interesting!
Justice