Reputation: 8188
I wanna write a linq query to do this
Select id from selectedBrands
where name='Nick' or name='Matt'
is this linq correct????
var brandsToNotShow=new[] {"Nick","Matt"};
model.Names=
(from s in selectedBrands
where brandsToNotShow.Any()
select s.Brand.name
).ToList();
where model.Names is a List
Upvotes: 0
Views: 158
Reputation: 126952
It's difficult to say what you want based on your choice of variable names, but the literal LINQ version of your SQL is
from brand in selectedBrands
where brand.name == 'Nick' || brand.name == 'Matt'
select brand.id;
If you had an array or sequnce in your code, you could use Contains
from brand in selectedBrands
where nameSequence.Contains(brand.name)
select brand.id;
But if that sequence is sizable, it is probably best to just join to the sequence.
from brand in selectedBrands
join name in nameSequence on brand.name equals name
select brand.id;
Upvotes: 1
Reputation: 91502
No, it is not correct.
brandsNotToShow.Any()
asks "are there any elements of this list", the answer to which is, yes, there are two elements.
This query is probably closer to what you want:
from s in selectedBrands
where brandsNotToShow.Contains(s.Brand.Name)
select s.Brand.Name
(actually, if it's brands NOT to show, it might be
where !brandsNotToShow.Contains(s.Brand.Name)
I can't tell from your example)
Upvotes: 5
Reputation: 85126
According to MSDN Any():
Determines whether any element of a sequence satisfies a condition.
So in your example Any
just checks to see if there are any items in brandsToNotSHow.
You probably want something like this:
from s in selectedBrands
where brantsToNotShow.Contains (s.Brand.Name)
SELECT s.Brand.Name
Upvotes: 1