find if an integer exists in a list of integers

C#ArraysInteger

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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);

Solution 4 - C#

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

Solution 5 - C#

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");
    }
}

Solution 6 - C#

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;
}

Solution 7 - C#

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

Solution 8 - C#

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleoraView Question on Stackoverflow
Solution 1 - C#Bobby BorszichView Answer on Stackoverflow
Solution 2 - C#Rune GrimstadView Answer on Stackoverflow
Solution 3 - C#DennisView Answer on Stackoverflow
Solution 4 - C#gsharpView Answer on Stackoverflow
Solution 5 - C#danialafshariView Answer on Stackoverflow
Solution 6 - C#eriksv88View Answer on Stackoverflow
Solution 7 - C#Woot4MooView Answer on Stackoverflow
Solution 8 - C#pintzView Answer on Stackoverflow