find if an integer exists in a list of integers

后端 未结 8 1332
执笔经年
执笔经年 2020-12-14 05:40

i have this code:

  List apps = getApps();

        List ids;

        List dropdown = apps.ConvertAll(c => new          


        
相关标签:
8条回答
  • 2020-12-14 05:52

    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");
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:00

    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
    
    0 讨论(0)
  • 2020-12-14 06:02

    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);
    
    0 讨论(0)
  • 2020-12-14 06:03

    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

    0 讨论(0)
  • 2020-12-14 06:05
    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;
    }
    
    0 讨论(0)
  • 2020-12-14 06:06
    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
    }
    
    0 讨论(0)
提交回复
热议问题