jeudi 1 mai 2014

c# - recherchant et ou de condition entre deux vérifications - Stack Overflow


Hi I have following linq statement


var list=(from c in db.sales 
where c.id ==id && || c.name==name
select new model
{
//.....

});

I am having problem with my where clause.


Basically I can get id and name values or either one of them can be null. So I am trying to use where clause with AND OR meaning both condition could be right or either one.


With this where syntax i get intelliscence error. Please let me know how I can use AND OR check in the where clause.





both condition could be right or either one



You simply need or and do not need and


var list=(from c in db.sales 
where c.id ==id || c.name==name
select new model
{
//.....

});

The || (or Operator)



The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false, MSDN.





It sounds like what you want is just an ordinary OR:


where c.id == id || c.name == name

This will evaluate to true if c.id == id is true, if c.name == name is true, or if both are true.




I think you mean IntelliSense error. This is obvious, because && || is not valid C# syntax.


From what you describe, I think you only need the OR operation.


Try changing && || to just ||.



Hi I have following linq statement


var list=(from c in db.sales 
where c.id ==id && || c.name==name
select new model
{
//.....

});

I am having problem with my where clause.


Basically I can get id and name values or either one of them can be null. So I am trying to use where clause with AND OR meaning both condition could be right or either one.


With this where syntax i get intelliscence error. Please let me know how I can use AND OR check in the where clause.




both condition could be right or either one



You simply need or and do not need and


var list=(from c in db.sales 
where c.id ==id || c.name==name
select new model
{
//.....

});

The || (or Operator)



The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false, MSDN.




It sounds like what you want is just an ordinary OR:


where c.id == id || c.name == name

This will evaluate to true if c.id == id is true, if c.name == name is true, or if both are true.



I think you mean IntelliSense error. This is obvious, because && || is not valid C# syntax.


From what you describe, I think you only need the OR operation.


Try changing && || to just ||.


0 commentaires:

Enregistrer un commentaire