none
linq query does not find the index of object from a particular list RRS feed

  • Question

  • I have a list of student type.I want to search intended student from the student list against studentID. My code snippet is given below

    int index = StudentList.FindIndex(x => x.studentID == studId);

    After execution of Linq Query ,I have 200 records in the student list when it applying the searching criteria on studentList it returns me unexpected results 

    Thursday, February 8, 2018 6:24 AM

All replies

  • Can you please proivde the expected and actual result?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 8, 2018 9:02 AM
  • Why do you need the index? LINQ pretty much eliminates the need for an index. You wouldn't use it to get back to the original item because you'd simply use FirstOrDefault(). Unless there is a specific need for an index, don't use it.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 8, 2018 3:12 PM
    Moderator
  • Hi TechLearn,

    What do you mean about "unexpected results"?

    I can get the correct result with the simple demo:

            List<Student> StudentList = new List<Student>();
            private void Form4_Load(object sender, EventArgs e)
            {
                Student student = new Student();
                student.StudentID = 1;
                student.Name = "N1";
                student.Age = 11;
                Student student2 = new Student();
                student2.StudentID = 2;
                student2.Name = "N2";
                student2.Age = 22;
                Student student3 = new Student();
                student3.StudentID = 3;
                student3.Name = "N3";
                student3.Age = 33;
    
                StudentList.Add(student); //order
                StudentList.Add(student2);
                StudentList.Add(student3);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int studId = 1;
                int index = StudentList.FindIndex(x => x.StudentID == studId);
                MessageBox.Show(index.ToString());
            }
    
            class Student
            {
                public int StudentID { get; set; }
                public string Name { get; set; }
                public int Age { get; set; }
            }

    The index is related to the order of addition(to StudentList), please check it.

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 9, 2018 2:48 AM