将自定义列表对象复制到另一个列表对象

时间:2016-07-06 05:41:29

标签: c# class pass-by-reference pass-by-value

我搜索了很多,我知道这个问题已被多次询问,但是找不到合适的解决方案。

我有一个具有以下结构的课程

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

我尝试这样做:

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

这里的问题是,当我从结果对象中删除项目时,它也会从模型中删除。我希望模型对象不会因结果对象的任何变化而受到影响。

我知道class默认是通过引用传递的,这就是它从对象中删除的原因。

让我知道如何通过可以解决问题的价值来传递它。

3 个答案:

答案 0 :(得分:1)

不应该:https://msdn.microsoft.com/en-us/library/fkbw11z0.aspx

但是。 尝试在List result = new List(model)之前克隆原始列表;

答案 1 :(得分:0)

感谢@David Pilkington

我已使用以下代码解决了我的问题。


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

我在现有代码中所做的唯一更改是将[Serializable]属性添加到我的类中。


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

答案 2 :(得分:0)

将列表作为参考类型遍历。

您需要再次创建

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

完整功能

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

现在,它会从 result 中删除该项目,而不是 model