none
How could I move a single object from one Lis<T> to another List<T> RRS feed

  • Question

  • I'm new to c#.

    My question is that how can I move one object from  my List<T> to another. The main list can be seen below.

    static List<Car> Cars = new List<Car>();

    I have a list of cars and my intention is to sell one of the cars.

    Car sell = Cars.Find(cr => cr.SecurityCode == index);

    I'm using the code above to find the object using the SecurityCode of the object.

    I want to remove this from the Cars list and then place it on a second List<Sell>. How would I do this?


    Umar Arif




    • Edited by StudiousStudent Saturday, April 20, 2019 12:37 PM Had to change code
    Saturday, April 20, 2019 12:35 PM

Answers

  • Hello,

    See the following.

    Define Car class, create two list.

    using System.Collections.Generic;
    
    namespace ForumQuestion
    {
        public class Car
        {
            public int id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
            public int Year { get; set; }
            public int Reg { get; set; }
            public override string ToString()
            {
                return $"{id}, {Reg}, {Make}, {Model},{Year}";
            }
        }
    
        public class Cars
        {
            public List<Car> CarList1 {
                get
                {
                    return new List<Car>()
                    {
                        new Car() {id = 1, Reg = 100, Make = "AAA", Model = "A1", Year = 2019},
                        new Car() {id = 2, Reg = 101, Make = "BBB", Model = "B1", Year = 2018},
                        new Car() {id = 3, Reg = 102, Make = "CCC", Model = "C1", Year = 2018}
                    };
                }
            }
            public List<Car> CarList2
            {
                get
                {
                    return new List<Car>()
                    {
                        new Car() {id = 4, Reg = 200, Make = "DDD", Model = "D1", Year = 2014},
                        new Car() {id = 5, Reg = 201, Make = "EEE", Model = "E1", Year = 2014},
                        new Car() {id = 6, Reg = 202, Make = "FFF", Model = "F1", Year = 2014}
                    };
                }
            }
        }
    }
    

    Do the move

    var carList = new Cars();
    
    var list1 = carList.CarList1;
    var list2 = carList.CarList2;
    
    var getCar = list1.FirstOrDefault(car => car.id == 2);
    if (getCar !=null)
    {
        list2.Add(getCar);
        list1.Remove(getCar);
    }
    else
    {
        // car not found
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, April 20, 2019 2:35 PM
    Moderator

All replies

  • static List<Car> Cars = new List<Car>();

    The above static list is not correct I think, becuase you are using the 'new' keyword in instancing the list into an object. Static means it doesn't need to be instanced into an object, but yet, you are instancing the list. The list should be public or private and not static.

    The List is strong typed to only contain Car objects, and the List for Sell is strong typed to only hold Sell objects. So you can't move the Car object to a List that is strong typed for a Sell object

    You can't tell a Car object that it's going to be a Sell object, becuase the class that defines the object is the blueprint for the object that has variables, properties and behavior/methods that are different between the two objects.

    It doesn't matter if it is .NET or Java, becuase OO is OO, they are both OO language platforms and the principles are the same. 

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Saturday, April 20, 2019 2:04 PM
  • Hello,

    See the following.

    Define Car class, create two list.

    using System.Collections.Generic;
    
    namespace ForumQuestion
    {
        public class Car
        {
            public int id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
            public int Year { get; set; }
            public int Reg { get; set; }
            public override string ToString()
            {
                return $"{id}, {Reg}, {Make}, {Model},{Year}";
            }
        }
    
        public class Cars
        {
            public List<Car> CarList1 {
                get
                {
                    return new List<Car>()
                    {
                        new Car() {id = 1, Reg = 100, Make = "AAA", Model = "A1", Year = 2019},
                        new Car() {id = 2, Reg = 101, Make = "BBB", Model = "B1", Year = 2018},
                        new Car() {id = 3, Reg = 102, Make = "CCC", Model = "C1", Year = 2018}
                    };
                }
            }
            public List<Car> CarList2
            {
                get
                {
                    return new List<Car>()
                    {
                        new Car() {id = 4, Reg = 200, Make = "DDD", Model = "D1", Year = 2014},
                        new Car() {id = 5, Reg = 201, Make = "EEE", Model = "E1", Year = 2014},
                        new Car() {id = 6, Reg = 202, Make = "FFF", Model = "F1", Year = 2014}
                    };
                }
            }
        }
    }
    

    Do the move

    var carList = new Cars();
    
    var list1 = carList.CarList1;
    var list2 = carList.CarList2;
    
    var getCar = list1.FirstOrDefault(car => car.id == 2);
    if (getCar !=null)
    {
        list2.Add(getCar);
        list1.Remove(getCar);
    }
    else
    {
        // car not found
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, April 20, 2019 2:35 PM
    Moderator
  • 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 22, 2019 1:26 AM
    Moderator