将自定义列表对象复制到另一个列表对象
Copy custom list object to another list object
我搜索了很多,我知道这个问题已经被问过很多次,但没有找到适合我的问题的解决方案。
我有一个 class 具有以下结构
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 默认情况下是按引用传递的,这就是它从两个对象中删除的原因。
让我知道如何让它按值传递,这可能会解决问题。
不应该:https://msdn.microsoft.com/en-us/library/fkbw11z0.aspx
但是。
尝试在 List result = new List(model); 之前克隆您的原始列表; .
感谢@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);
}
}
}
我对现有代码所做的唯一更改是像这样向我的 class 添加 [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; }
}
列表作为引用类型遍历。
您需要重新创建一次
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
.
我搜索了很多,我知道这个问题已经被问过很多次,但没有找到适合我的问题的解决方案。
我有一个 class 具有以下结构
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 默认情况下是按引用传递的,这就是它从两个对象中删除的原因。
让我知道如何让它按值传递,这可能会解决问题。
不应该:https://msdn.microsoft.com/en-us/library/fkbw11z0.aspx
但是。 尝试在 List result = new List(model); 之前克隆您的原始列表; .
感谢@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); } } }
我对现有代码所做的唯一更改是像这样向我的 class 添加 [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; } }
列表作为引用类型遍历。
您需要重新创建一次
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
.