none
How to remove a complex List of object from a List ? RRS feed

  • Question

  • Hi All,

    I have a list which contains a list of object and that object also contain some List property, I want to remove one list item from that list.

    Let's take an example,

    Suppose i have a  list of student defined as list<student> lststudent=new list<student>

    The student model also contains many list property where the data are binding.

    I want to remove a particular item as per my condition from the list of item.

    Scenario:-

    Basically what happens is when the data comes inside the list there is some list item which is duplicate, that I need to find out from the list and remove it but before remove I need to take out that itemType property from the duplicate object and place into another property.

    To achieve the above scenario I have did below thing, but nothing is working,

      class ListObjectRemove
        {
            public static void RemoveItem()
            {
                var objStudent = GetAllStudent();
                int check = 0;
                for (int i = 0; i <objStudent.Count() ; i++)
                {
                    objStudent[i].lstStype = new List<int>();
                    for (int j = 1; j < objStudent.Count(); j++)
                    {
                        if (objStudent[i].sGlobalId == objStudent[j].sGlobalId)
                        {
                            objStudent[i].lstStype.Add(objStudent[j].sGlobalId);
                            objStudent.Remove(objStudent[j]);    //This is not working
                            //objStudent.RemoveAt(j); 
                            check++;
                        }
                    }
                    if (check>0)
                    {
                        objStudent[i].lstStype.Add(objStudent[i].sGlobalId);
                    }
                }
            }
    
            private static List<Student> GetAllStudent()
            {
                throw new NotImplementedException();
            }
        }

    Model Class :

      class Student
        {
            public int id { get; set; }
            public string Name { get; set; }
            public List<Teacher> lstTeacher { get; set; }
            public int sType { get; set; }
            public List<int> lstStype { get; set; }
            public int sGlobalId { get; set; }
        }
        class Teacher
        {
            public int tId { get; set; }
            public string name { get; set; }
        }

    Above model is a simple one but in my real application, it contains many list property inside it and other property.

    After all the operation has done I need to pass the updated list to method, but above code is not working.

    Please share your ideas to solve this.

    Thanks,

    SK.


     

    S.K Nayak




    Friday, April 12, 2019 2:46 AM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to remove a complex List of object from a List.

    You could try the following code, the bold code is modified.

    class ListObjectRemove
        {
            public static void RemoveItem()
            {
                var objStudent = GetAllStudent();
                int check = 0;
                for (int i = 0; i < objStudent.Count(); i++)
                {
                    objStudent[i].lstStype = new List<int>();
                    for (int j = 0; j < objStudent.Count(); j++)
                    {
                        if ((objStudent[i].sGlobalId == objStudent[j].sGlobalId)&&(i!=j))
                        {
                            objStudent[i].lstStype.Add(objStudent[j].sGlobalId);
                            objStudent.Remove(objStudent[j]);   // delete duplicate data 4
                            check++;
                        }
                    }
                    if (check > 0)
                    {
                        objStudent[i].lstStype.Add(objStudent[i].sGlobalId); // add data 4
                    }
                }
                foreach (var item in objStudent)
                {
                    Console.WriteLine(item.sGlobalId);   
    
                }
                Console.WriteLine("***************");
                foreach (var item in objStudent)
                {
                    //Console.WriteLine(item.sGlobalId); 
    
                    foreach (var i in item.lstStype)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
    
            private static List<Student> GetAllStudent()
            {
                List<Student> list = new List<Student>();
                List<Teacher> teachers = new List<Teacher>();
                teachers.Add(new Teacher { tId = 1001, name = "kitty" });
                teachers.Add(new Teacher { tId = 1002, name = "anny" });
                teachers.Add(new Teacher { tId = 1003, name = "betty" });
                List<int> type = new List<int>();
               type.Add(1);
                type.Add(2);
                list.Add(new Student { id = 001, Name = "JAck", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 4 });
                list.Add(new Student { id = 002, Name = "Jhon", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 6 });
                list.Add(new Student { id = 003, Name = "Simon", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 4 });
                return list;
            }
        }
        class Student
        {
            public int id { get; set; }
            public string Name { get; set; }
            public List<Teacher> lstTeacher { get; set; }
            public int sType { get; set; }
            public List<int> lstStype { get; set; }
            public int sGlobalId { get; set; }
        }
        class Teacher
        {
            public int tId { get; set; }
            public string name { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ListObjectRemove.RemoveItem();
                Console.ReadKey();
            }
    }
    

    Result:

    Best Regards,

    Jack



    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, April 12, 2019 6:30 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    For your question, you want to remove a complex List of object from a List.

    You could try the following code, the bold code is modified.

    class ListObjectRemove
        {
            public static void RemoveItem()
            {
                var objStudent = GetAllStudent();
                int check = 0;
                for (int i = 0; i < objStudent.Count(); i++)
                {
                    objStudent[i].lstStype = new List<int>();
                    for (int j = 0; j < objStudent.Count(); j++)
                    {
                        if ((objStudent[i].sGlobalId == objStudent[j].sGlobalId)&&(i!=j))
                        {
                            objStudent[i].lstStype.Add(objStudent[j].sGlobalId);
                            objStudent.Remove(objStudent[j]);   // delete duplicate data 4
                            check++;
                        }
                    }
                    if (check > 0)
                    {
                        objStudent[i].lstStype.Add(objStudent[i].sGlobalId); // add data 4
                    }
                }
                foreach (var item in objStudent)
                {
                    Console.WriteLine(item.sGlobalId);   
    
                }
                Console.WriteLine("***************");
                foreach (var item in objStudent)
                {
                    //Console.WriteLine(item.sGlobalId); 
    
                    foreach (var i in item.lstStype)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
    
            private static List<Student> GetAllStudent()
            {
                List<Student> list = new List<Student>();
                List<Teacher> teachers = new List<Teacher>();
                teachers.Add(new Teacher { tId = 1001, name = "kitty" });
                teachers.Add(new Teacher { tId = 1002, name = "anny" });
                teachers.Add(new Teacher { tId = 1003, name = "betty" });
                List<int> type = new List<int>();
               type.Add(1);
                type.Add(2);
                list.Add(new Student { id = 001, Name = "JAck", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 4 });
                list.Add(new Student { id = 002, Name = "Jhon", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 6 });
                list.Add(new Student { id = 003, Name = "Simon", lstTeacher = teachers, lstStype = type, sType = 3, sGlobalId = 4 });
                return list;
            }
        }
        class Student
        {
            public int id { get; set; }
            public string Name { get; set; }
            public List<Teacher> lstTeacher { get; set; }
            public int sType { get; set; }
            public List<int> lstStype { get; set; }
            public int sGlobalId { get; set; }
        }
        class Teacher
        {
            public int tId { get; set; }
            public string name { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ListObjectRemove.RemoveItem();
                Console.ReadKey();
            }
    }
    

    Result:

    Best Regards,

    Jack



    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, April 12, 2019 6:30 AM
    Moderator
  • There are a couple of issues here.  You have i running from 0 to objStudent.Count(), and j running from 1 to objStudent.Count().  The second time through the outer loop, i will be 1, but you start the loop with j=1, and it's pretty clear that objStudent[1] will be equal to objStudent[1].  Thus, you will eventually remove every element except the first, because each i will match itself.  The solution to that is to have the inner loop start at (i+1) instead of 1.

    Next, it's a little dangerous to modify a list while you are iterating through it.  My suggestion is to use a List<int> to keep track of the item numbers you WANT to delete, then after the outer loop, run through that list and remove those elements.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, April 12, 2019 9:40 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    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.

    Monday, April 15, 2019 5:36 AM
    Moderator
  • Thank you all for your help, i marked the post as answer.

    S.K Nayak

    Monday, April 22, 2019 6:17 AM