i have this code:
List<T> apps = getApps(); List<int> ids; List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem { Selected = ids.Contains(c.Id), Text = c.Name, Value = c.Id.ToString() }).ToList(); ids.Contains
seems to always return false even though the numbers do match
any ideas?
You can use List. Contains() method. Determines whether an element is in the List<T> .
We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.
As you would expect, we can also assign list values to variables and pass lists as parameters to functions. A list can contain only integer items.
If you just need a true/false result
bool isInList = intList.IndexOf(intVariable) != -1;
if the intVariable does not exist in the List it will return -1
As long as your list is initialized with values and that value actually exists in the list, then Contains should return true.
I tried the following:
var list = new List<int> {1,2,3,4,5}; var intVar = 4; var exists = list.Contains(intVar);
And exists is indeed set to true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With