c#在嵌套对象列表中插入项
我有这样的课:c#在嵌套对象列表中插入项,c#,list,linq,lambda,nested,C#,List,Linq,Lambda,Nested,我有这样的课: public class Abc { public int Id { get; set; } public List<Abc> Child{ get; set; } } ==更新==== 话虽如此,我有两个输入参数: Id=4 Abc类的对象(Id=9) Abc类的对象(Id=11) 我需要在整个列表中找到id为4的对象,并且仅当该对象在列表中不存在时才将该对象插入其子对象中 因此id=9已经存在,因此无法插入。但是我可以插入Id=11的对象
public class Abc
{
public int Id { get; set; }
public List<Abc> Child{ get; set; }
}
==更新====
话虽如此,我有两个输入参数:
因此,添加id=11后,列表应该如下所示
Abc (Id = 1)
|
|-- Abc (Id = 2)
|-- Abc (Id = 3)
|
|-- Abc (Id = 4)
|
|-- Abc (Id =5)
|-- Abc (Id = 11) -- ADDED
Abc (Id = 6)
|
|-- Abc (Id = 7)
|-- Abc (Id = 8)
|
|-- Abc (Id = 9)
|
|-- (Id = 10)
我不知道如何做到这一点。
有人能帮忙吗?它可以是c#lambda或linq或任何其他方法。尝试递归,应该可以正常工作。在每个成员处检查其ID。如果它是您想要的ID,只需将对象插入其子对象。如果没有,请检查它是否有子对象。如果没有,请转到下一个元素。如果是的话,请按照检查当前成员的相同方式检查其子级(这就是递归的作用)
为了练习,您可以自己编写代码。请随意询问它是否工作。尝试递归,应该可以正常工作。在每个成员处检查其ID。如果它是您想要的ID,只需将对象插入其子对象。如果没有,请检查它是否有子对象。如果没有,请转到下一个元素。如果是的话,请按照检查当前成员的相同方式检查其子级(这就是递归的作用)
为了练习,您可以自己编写代码。请随意询问它是否有效。类似的方法应该有效:
public void findIndex(Abc myClass)
{
if(myClass.Id == 4)
{
//Insert the new item here in the children childs
}
else
{
Foreach(Abc children in myClass.Child)
{
findIndex(children)
}
}
}
类似的方法应该会奏效:
public void findIndex(Abc myClass)
{
if(myClass.Id == 4)
{
//Insert the new item here in the children childs
}
else
{
Foreach(Abc children in myClass.Child)
{
findIndex(children)
}
}
}
当然,您可以检查子属性不为null的位置
public class Abc
{
public Abc(int id)
{
Id = id;
}
public int Id { get; }
public List<Abc> Child { get; set; }
public Abc FindById(int id)
{
if (Id == id) return this;
if (Child == null) return null;
foreach (var childItem in Child)
{
var result = childItem.FindById(id);
if (result != null) return result;
}
return null;
}
public bool HasChild(int id)
{
return FindById(id) != null;
}
public Abc AddChildIfNotExist(Abc child)
{
if (child == null) return this;
if (!HasChild(child.Id))Child.Add(child);
return this;
}
}
static void Main(string[] args)
{
var a = new Abc(1)
{
Child = new List<Abc>
{
new Abc(2),
new Abc(3)
{
Child = new List<Abc>
{
new Abc(7)
{
Child = new List<Abc>
{
new Abc(5)
}
}
}
},
new Abc(6)
{
Child = new List<Abc>
{
new Abc(8)
{
Child = new List<Abc>
{
new Abc(9),
new Abc(4)
{
Child = new List<Abc>()
}
}
}
}
}
}
};
a
.FindById(4)
.AddChildIfNotExist(new Abc(10))
.AddChildIfNotExist(new Abc(4));
}
公共类Abc
{
公共Abc(内部id)
{
Id=Id;
}
公共int Id{get;}
公共列表子项{get;set;}
公共Abc FindById(内部id)
{
如果(Id==Id)返回这个;
if(Child==null)返回null;
foreach(子对象中的var childItem)
{
var result=childItem.FindById(id);
if(result!=null)返回结果;
}
返回null;
}
公共bool HasChild(int-id)
{
返回FindById(id)!=null;
}
公共Abc AddChildIfNotExist(Abc儿童)
{
如果(child==null)返回此值;
如果(!HasChild(child.Id))child.Add(child);
归还这个;
}
}
静态void Main(字符串[]参数)
{
var a=新Abc(1)
{
Child=新列表
{
新Abc(2),
新Abc(3)
{
Child=新列表
{
新Abc(7)
{
Child=新列表
{
新Abc(5)
}
}
}
},
新Abc(6)
{
Child=新列表
{
新Abc(8)
{
Child=新列表
{
新Abc(9),
新Abc(4)
{
Child=新列表()
}
}
}
}
}
}
};
A.
.FindById(4)
.AddChildIfNotExist(新Abc(10))
.AddChildIfNotExist(新Abc(4));
}
当然,您可以检查Child属性不为null的位置
public class Abc
{
public Abc(int id)
{
Id = id;
}
public int Id { get; }
public List<Abc> Child { get; set; }
public Abc FindById(int id)
{
if (Id == id) return this;
if (Child == null) return null;
foreach (var childItem in Child)
{
var result = childItem.FindById(id);
if (result != null) return result;
}
return null;
}
public bool HasChild(int id)
{
return FindById(id) != null;
}
public Abc AddChildIfNotExist(Abc child)
{
if (child == null) return this;
if (!HasChild(child.Id))Child.Add(child);
return this;
}
}
static void Main(string[] args)
{
var a = new Abc(1)
{
Child = new List<Abc>
{
new Abc(2),
new Abc(3)
{
Child = new List<Abc>
{
new Abc(7)
{
Child = new List<Abc>
{
new Abc(5)
}
}
}
},
new Abc(6)
{
Child = new List<Abc>
{
new Abc(8)
{
Child = new List<Abc>
{
new Abc(9),
new Abc(4)
{
Child = new List<Abc>()
}
}
}
}
}
}
};
a
.FindById(4)
.AddChildIfNotExist(new Abc(10))
.AddChildIfNotExist(new Abc(4));
}
公共类Abc
{
公共Abc(内部id)
{
Id=Id;
}
公共int Id{get;}
公共列表子项{get;set;}
公共Abc FindById(内部id)
{
如果(Id==Id)返回这个;
if(Child==null)返回null;
foreach(子对象中的var childItem)
{
var result=childItem.FindById(id);
if(result!=null)返回结果;
}
返回null;
}
公共bool HasChild(int-id)
{
返回FindById(id)!=null;
}
公共Abc AddChildIfNotExist(Abc儿童)
{
如果(child==null)返回此值;
如果(!HasChild(child.Id))child.Add(child);
归还这个;
}
}
静态void Main(字符串[]参数)
{
var a=新Abc(1)
{
Child=新列表
{
新Abc(2),
新Abc(3)
{
Child=新列表
{
新Abc(7)
{
Child=新列表
{
新Abc(5)
}
}
}
},
新Abc(6)
{
Child=新列表
{
新Abc(8)
{
Child=新列表
{
新Abc(9),
新Abc(4)
{
Child=新列表()
}
}
}
}
}
}
};
A.
.FindById(4)
.AddChildIfNotExist(新Abc(10))
.AddChildIfNotExist(新Abc(4));
}
这是你经常做的事还是只做一次?如果你只做一次,那么在你的列表中循环可能就是最好的方法。如果您经常这样做,那么您可能希望存储一个ABC字典,并将它们的id作为键,这样您就可以非常方便地找到特定的ABC