我搜索了很多,我知道這個問題已被問了很多次,但沒有找到適合我的問題的解決方案。複製自定義列表對象到另一個列表對象
我有一類具有下列結構
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);
}
問題在這裏是當我刪除結果對象項目它會從模型中移除,以及。我希望模型對象不會因結果對象中的任何更改而受到影響。
我知道類是默認傳遞引用,這就是爲什麼它從兩個對象中刪除。
讓我知道如何讓它通過值可能解決問題。
http://stackoverflow.com/questions/78536/deep-cloning-objects –