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?
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
You should be referencing Selected
not ids.Contains
as the last line.
I just realized this is a formatting issue, from the OP. Regardless you should be referencing the value in Selected. I recommend adding some Console.WriteLine calls to see exactly what is being printed out on each line and also what each value is.
After your update: ids is an empty list, how is this not throwing a NullReferenceException? As it was never initialized in that code block
string name= "abc";
IList<string> strList = new List<string>() { "abc", "def", "ghi", "jkl", "mno" };
if (strList.Contains(name))
{
Console.WriteLine("Got It");
}
///////////////// OR ////////////////////////
IList<int> num = new List<int>();
num.Add(10);
num.Add(20);
num.Add(30);
num.Add(40);
Console.WriteLine(num.Count); // to count the total numbers in the list
if(num.Contains(20)) {
Console.WriteLine("Got It"); // if condition to find the number from list
}
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.
The best of code and complete is here:
NumbersList.Exists(p => p.Equals(Input)
Use:
List<int> NumbersList = new List<int>();
private void button1_Click(object sender, EventArgs e)
{
int Input = Convert.ToInt32(textBox1.Text);
if (!NumbersList.Exists(p => p.Equals(Input)))
{
NumbersList.Add(Input);
}
else
{
MessageBox.Show("The number entered is in the list","Error");
}
}
bool vExist = false;
int vSelectValue = 1;
List<int> vList = new List<int>();
vList.Add(1);
vList.Add(2);
IEnumerable vRes = (from n in vListwhere n == vSelectValue);
if (vRes.Count > 0) {
vExist = true;
}
Here is a extension method, this allows coding like the SQL IN command.
public static bool In<T>(this T o, params T[] values)
{
if (values == null) return false;
return values.Contains(o);
}
public static bool In<T>(this T o, IEnumerable<T> values)
{
if (values == null) return false;
return values.Contains(o);
}
This allows stuff like that:
List<int> ints = new List<int>( new[] {1,5,7});
int i = 5;
bool isIn = i.In(ints);
Or:
int i = 5;
bool isIn = i.In(1,2,3,4,5);
The way you did is correct. It works fine with that code: x is true. probably you made a mistake somewhere else.
List<int> ints = new List<int>( new[] {1,5,7}); // 1
List<int> intlist=new List<int>() { 0,2,3,4,1}; // 2
var i = 5;
var x = ints.Contains(i); // return true or false
Source: Stackoverflow.com