Copy custom list object to another list object

Sham

I searched a lot and I know this question has been asked many times, but don't find proper solution for my issue.

I have a class with following structure

namespace Website.Models
{
    public class DynamicControlModel
    {
    }

    public class QuestionnairModel
    {
        public long QuestionId { get; set; }
        public string Question { get; set; }
        public List<QuestionnairOptionModel> QuestionnairOption { get; set; }
    }

    public class QuestionnairOptionModel
    {
        public long OptionId { get; set; }
        public string OptionString { get; set; }
        public bool OptionControl1 { get; set; }
        public string OptionControl2 { get; set; }
    }
}

and I try to do this :

public ActionResult ProcessRequest(List<QuestionnairModel> model)
{
    List<QuestionnairModel> result = new List<QuestionnairModel>(model);
    result = result.Where(x => x.QuestionnairOption.Any(l => l.OptionControl1 == true)).ToList();
    result.ForEach(x => x.QuestionnairOption.RemoveAll(m => m.OptionControl1 == false));
    return View(@"~\Views\Home\About.cshtml", model);
}

Issue over here is when i remove item from result object it gets removed from model as well. I want model object should not get affected due to any change in result object.

I know class is by default pass by reference and that's why it gets removed from both the object.

Let me know how can i make it Pass by value which might solve the issue.

Sham

Thanks to @David Pilkington

I have resolved my issue using below code.

     public static class ObjectCopier
    {
        /// 
        /// Perform a deep Copy of the object.
        /// 
        /// The type of object being copied.
        /// The object instance to copy.
        /// The copied object.
        public static T Clone(T source)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            // Don't serialize a null object, simply return the default for that object
            if (Object.ReferenceEquals(source, null))
            {
                return default(T);
            }

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();
            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }
    }

The only change i did in my existing code is added [Serializable] attribute to my class like this.

     [Serializable]
    public class QuestionnairModel
    {
        public long QuestionId { get; set; }
        public string Question { get; set; }
        public List QuestionnairOption { get; set; }
    }

    [Serializable]
    public class QuestionnairOptionModel
    {
        public long OptionId { get; set; }
        public string OptionString { get; set; }
        public bool OptionControl1 { get; set; }
        public string OptionControl2 { get; set; }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Automapper not copy list object to another

From Dev

Copy list of of objects to another list of another objects within another object

From Dev

Copy an object from a list into another object in the same list

From Dev

Map a list of object to another list using Dozer's custom converters

From Dev

Parcelable List that contains a custom object

From Dev

Insert a custom object in a sorted list

From Dev

Add custom object list to Redis

From Dev

Parcelable List that contains a custom object

From Dev

linq manipulate list of custom object

From Dev

How to cast an custom object to List<object>

From Dev

Check if element is list or another object

From Dev

Compare list of strings with object properties in another list

From Dev

Look for a string object from a list in another list

From Dev

Convert one Optional<List<Object>> to another Optional<List<Object>> in Java

From Java

How to check if an object of one list contains any object of another list

From Dev

Remove duplicate list object value from another object list

From Dev

How to convert list of object to another list object using streams?

From Dev

C# Sorting a List of an Object which contains another List of an Object

From Dev

How to access a generic list and assign the object of generic list to another object

From Dev

Automapper how to map list of one object to list of another object

From Dev

Java ResultSet copy to generic object or list

From Dev

AttributeError: 'list' object has no attribute 'copy'

From Dev

Java ResultSet copy to generic object or list

From Dev

Copy List<Object> with "addAll" method NoSuchElementException

From Dev

Trying to copy object in different list with Realm in Android

From Dev

AttributeError: 'list' object has no attribute 'copy'

From Dev

Filtering a list of list of object with another list in Java 8

From Dev

JAX-RS consuming a custom object list inside a custom object

From Dev

Copy data from a List of object to a different Object in C#

Related Related

  1. 1

    Automapper not copy list object to another

  2. 2

    Copy list of of objects to another list of another objects within another object

  3. 3

    Copy an object from a list into another object in the same list

  4. 4

    Map a list of object to another list using Dozer's custom converters

  5. 5

    Parcelable List that contains a custom object

  6. 6

    Insert a custom object in a sorted list

  7. 7

    Add custom object list to Redis

  8. 8

    Parcelable List that contains a custom object

  9. 9

    linq manipulate list of custom object

  10. 10

    How to cast an custom object to List<object>

  11. 11

    Check if element is list or another object

  12. 12

    Compare list of strings with object properties in another list

  13. 13

    Look for a string object from a list in another list

  14. 14

    Convert one Optional<List<Object>> to another Optional<List<Object>> in Java

  15. 15

    How to check if an object of one list contains any object of another list

  16. 16

    Remove duplicate list object value from another object list

  17. 17

    How to convert list of object to another list object using streams?

  18. 18

    C# Sorting a List of an Object which contains another List of an Object

  19. 19

    How to access a generic list and assign the object of generic list to another object

  20. 20

    Automapper how to map list of one object to list of another object

  21. 21

    Java ResultSet copy to generic object or list

  22. 22

    AttributeError: 'list' object has no attribute 'copy'

  23. 23

    Java ResultSet copy to generic object or list

  24. 24

    Copy List<Object> with "addAll" method NoSuchElementException

  25. 25

    Trying to copy object in different list with Realm in Android

  26. 26

    AttributeError: 'list' object has no attribute 'copy'

  27. 27

    Filtering a list of list of object with another list in Java 8

  28. 28

    JAX-RS consuming a custom object list inside a custom object

  29. 29

    Copy data from a List of object to a different Object in C#

HotTag

Archive