Can I perform an "or" operation in LINQ for objects?

Finding LINQ and Or on google is a bit complicated, so I'm here.

I want to do 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)
+3
source share
3 answers

C # keywords supporting LINQ are still C #. Consider whereas a condition similar if; you perform logical operations in the same way. In this case, 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)
+7
source

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());
+3
source

, . | #

msdn | msdn ||

, ( , ). , .

, , , .

The reason I prefer single pipes in linq queries is this: if a query is ever used in linq for sql, the basic translation of both C # OR statements is sql OR, which works as a single channel.

+1
source

Source: https://habr.com/ru/post/1698935/


All Articles