This code obviously doesn't work...but it's what I want to accomplish (Using IList)
if (this.Model.myOptionsList.Contains(SelectedOption.value)){
do something
}
The contents of myOptionsList looks like this:
SelectedOption.value contains an Id...i.e. 34 I need to know if that exists in the myOptionsList.
The catch is that I need to do that on the Razor page because it won't be available in the controller.
There are many methods, two friends said. I say three other methods(Count, Exists, FirstOrDefault).
Exists(it is better):
if (this.Model.myOptionsList.Exists(d => d.Id == SelectedOption.value))
{
// do something
}
Count:
if (this.Model.myOptionsList.Count(d => d.Id == SelectedOption.value) > 0)
{
// do something
}
FirstOrDefault:
if (this.Model.myOptionsList.FirstOrDefault(d => d.Id == SelectedOption.value) != null)
{
// do something
}
also you can write if in two ways. The first method, if the condition is false, return; Next go to second part( // do something) way 1:
if (this.Model.myOptionsList.Count(d => d.Id == SelectedOption.value) == 0) return;
//other
// do something
way 2:
if (this.Model.myOptionsList.Count(d => d.Id == SelectedOption.value) == 0)
{
// do something
}