C# 在列表中设置null会导致其他列表中的值为null
我有两个列表,我想从中获取不同的项目C# 在列表中设置null会导致其他列表中的值为null,c#,collections,C#,Collections,我有两个列表,我想从中获取不同的项目 SearchElement[] criteria = new SearchElement[] { new SearchElement { Comparison = "=", FieldName = "CableProperty.ProjectId", FieldValue = int.Parse(comboBoxSource.SelectedValue.ToString()),
SearchElement[] criteria = new SearchElement[] {
new SearchElement
{
Comparison = "=",
FieldName = "CableProperty.ProjectId",
FieldValue = int.Parse(comboBoxSource.SelectedValue.ToString()),
LogicalOperator = "" }
};
sourceCables = client.GetCables(criteria, null, "Cores,CableProperty,CableProperty.CableApplication").ToList();
criteria = new SearchElement[] {
new SearchElement
{
Comparison = "=",
FieldName = "CableProperty.ProjectId",
FieldValue = int.Parse(comboBoxDestination.SelectedValue.ToString()),
LogicalOperator = "" }
};
destinationCables = client.GetCables(criteria, null, "Cores,CableProperty,CableProperty.CableApplication").ToList();
diffCables = sourceCables.Except(destinationCables, new CableComparer())
.ToList();
现在我在diffcable
中有了不同的项目。有时我想设置
diffCable.CableProperty.CableApplication=null代码>
但当我这样做时,sourcelist中的所有导航属性(CableApplication)也设置为null。
这是密码
if (destinationCableApplications.Contains(diffCable.CableProperty.CableApplication, new CableApplicationComparer()))
{
criteria = new SearchElement[] { new SearchElement { Comparison = "=", FieldName = "ProjectId", FieldValue = int.Parse(comboBoxDestination.SelectedValue.ToString()), LogicalOperator = "" }};
cableApplication = client.GetCableApplications(criteria, null, "").SingleOrDefault();
diffCable.CableProperty.CableApplication = null;
}
很抱歉,在这一行之后
diffCable.CableProperty.CableApplication = null;
所有的
sourcecables[0].CableProperty.CableApplication
sourcecables[1].CableProperty.CableApplication
.....
sourcecables[100].CableProperty.CableApplication
设置为空
当我将diffcable中的navigation属性设置为null时,我应该如何做才能不丢失sourcelist中的navigation属性?您正在通过引用进行复制,但没有意识到这一点。研究克隆对象或创建新列表
List<int> newCopyList = new List<int>(originalList);
List newCopyList=新列表(原始列表);
最简单的方法是使用MemoryStream。。
这是一个样本
[Serializable]
public class temp
{
public int a;
}
class Program
{
public static T DeepClone<T>(T a)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, a);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
static void Main(string[] args)
{
List<temp> list1 = new List<temp>();
list1.Add(new temp { a = 1 });
list1.Add(new temp { a = 2 });
list1.Add(new temp { a = 3 });
List<temp> list2 = DeepClone<List<temp>>(list1);
list1[1].a = 4;
Console.WriteLine(list2[1].a);
Console.ReadKey();
}
}
[可序列化]
公共类临时工
{
公共INTA;
}
班级计划
{
公共静态T深度克隆(T a)
{
使用(MemoryStream stream=new MemoryStream())
{
BinaryFormatter formatter=新的BinaryFormatter();
序列化(流,a);
流位置=0;
返回(T)格式化程序。反序列化(流);
}
}
静态void Main(字符串[]参数)
{
List list1=新列表();
列表1.Add(新的temp{a=1});
列表1.Add(新的temp{a=2});
列表1.Add(新的temp{a=3});
List list2=深度克隆(list1);
列表1[1]。a=4;
Console.WriteLine(列表2[1].a);
Console.ReadKey();
}
}
注意:类必须是可序列化的。
这将适用于所有值和引用类型。对列表进行深度复制,而不是像Arun所说的那样进行浅层复制,这样的对象是无法复制的。复制引用时,两个列表中都是同一个对象。您需要使用相同的数据创建一个新对象,并将其放入新列表中。深度复制是什么意思?在内部,当您写入List1=List2时,它们的工作方式与数组类似;它只将List2的第一个地址传递给List1变量。这是浅拷贝,因为现在两个变量都指向相同的物理地址,您需要的是为List1创建单独的内存块,它将保存List2项的值(而不是引用),即。DeepCopy@nnmmss在C#中处理深度拷贝的正确方法是让您的Cable类实现ICloneable
。然后它实现一个Clone()
方法,该方法创建一个新对象并复制原始对象的所有属性。