0

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.

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
Sham
  • 691
  • 2
  • 13
  • 26

3 Answers3

1

It shouldn't : https://msdn.microsoft.com/en-us/library/fkbw11z0.aspx

But. Try to clone your original list before List result = new List(model); .

Dexion
  • 1,101
  • 8
  • 14
0

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; }
    }

Sham
  • 691
  • 2
  • 13
  • 26
0

The List is traversed as a Reference Type.

You need to create it once again

List<QuestionnairModel> result = new List<QuestionnairModel>(
                                    model.Select(m => new QuestionnairModel()
                                        {
                                            Question = m.Question,
                                            QuestionId = m.QuestionId,
                                            QuestionnairOption = new List<QuestionnairOptionModel>(
                                                    m.QuestionnairOption.Select(n => new QuestionnairOptionModel()
                                                    {
                                                        OptionId = n.OptionId,
                                                        OptionString = n.OptionString,
                                                        OptionControl1 = n.OptionControl1,
                                                        OptionControl2 = n.OptionControl2
                                                    }).ToList()
                                                )
                                        }).ToList()
                                    );

The Complete Functionality is

public ActionResult ProcessRequest(List<QuestionnairModel> model)
{

    List<QuestionnairModel> result = new List<QuestionnairModel>(
                                        model.Select(m => new QuestionnairModel()
                                            {
                                                Question = m.Question,
                                                QuestionId = m.QuestionId,
                                                QuestionnairOption = new List<QuestionnairOptionModel>(
                                                        m.QuestionnairOption.Select(n => new QuestionnairOptionModel()
                                                        {
                                                            OptionId = n.OptionId,
                                                            OptionString = n.OptionString,
                                                            OptionControl1 = n.OptionControl1,
                                                            OptionControl2 = n.OptionControl2
                                                        }).ToList()
                                                    )
                                            }).ToList()
                                        );



    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);
}

Now, it will remove the item from the result not from the model.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130