c#类中设置值的最佳最短方法
检查下面的代码。这里我想设置c#类中设置值的最佳最短方法,c#,C#,检查下面的代码。这里我想设置根类的值以及它的所有子列表值。我已经像贝娄一样尝试过了,但我能够为根这样做,但我如何为所有子类中的对象设置值呢 模型类: namespace MyProject.ViewModels { public class Child3 { public int Id { get; set; } public string text { get; set; } } public class Child2
根类的值以及它的所有子列表值。我已经像贝娄一样尝试过了,但我能够为根
这样做,但我如何为所有子类中的对象设置值呢
模型类:
namespace MyProject.ViewModels
{
public class Child3
{
public int Id { get; set; }
public string text { get; set; }
}
public class Child2
{
public int Id { get; set; }
public string text { get; set; }
public List<Child3> children { get; set; }
}
public class Child
{
public int Id { get; set; }
public string text { get; set; }
public List<Child2> children { get; set; }
}
public class Root
{
public int Id { get; set; }
public string text { get; set; }
public List<Child> children { get; set; }
}
}
namespace MyProject.ViewModels
{
公营儿童3
{
公共int Id{get;set;}
公共字符串文本{get;set;}
}
公营儿童2
{
公共int Id{get;set;}
公共字符串文本{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
公共int Id{get;set;}
公共字符串文本{get;set;}
公共列表子项{get;set;}
}
公共类根
{
公共int Id{get;set;}
公共字符串文本{get;set;}
公共列表子项{get;set;}
}
}
正在尝试设置值:
var root = new List<Root>();
root.Add(new Root
{
Id = 1,
text = "foo",
children = new List<Child>()
{
//i am not getting idea from here.
},
});
var root=newlist();
添加(新根)
{
Id=1,
text=“foo”,
children=新列表()
{
//我不知道从这里。
},
});
试试这个
var root = new List<Root>();
root.Add(new Root
{
Id = 1,
text = "foo",
children = new List<Child>()
{
new Child
{...},
new Child
{...},
new Child
{...}
},
});
var root=新列表();
添加(新根)
{
Id=1,
text=“foo”,
children=新列表()
{
新生儿
{...},
新生儿
{...},
新生儿
{...}
},
});
你被困的地方应该有一堆子对象
ren,所以创建一堆子对象
对象
children = new List<Child>
{
new Child {
...
},
new Child {
...
},
...
}
你应该充分利用C
首先,考虑更新你的模型来自动实例化他们的列表。例如:
public class Root
{
public int Id { get; set; }
public string text { get; set; }
public List<Child> children { get; set; } = new List<Child>();
}
如果不希望自动实例化集合,则需要在每个级别实例化它们:
var root = new List<Root>
{
new Root
{
Id = 1,
text = "foo",
children = new List<Child>
{
new Child
{
// ...
},
new Child
{
// ...
},
}
}
};
var root=新列表
{
新根
{
Id=1,
text=“foo”,
儿童=新列表
{
新生儿
{
// ...
},
新生儿
{
// ...
},
}
}
};
var root=new List();
添加(新根)
{
Id=1,
text=“foo”,
children=新列表()
{
新生儿{
Id=1,
text=“子文本”,
children=新列表(){
新生儿2{
Id=1,
text=“子文本”,
儿童=新列表{
新生儿3{
Id=1,
text=“子文本”
},
新生儿3{
Id=2,
text=“子文本”
}
}
},
新生儿2{
Id=2,
text=“子文本”,
儿童=新列表{
新生儿3{
Id=3,
text=“子文本”
},
新生儿3{
Id=4,
text=“子文本”
}
}
}
}
},
//儿童2
新生儿{
Id=1,
text=“子文本”,
children=新列表(){
新生儿2{
Id=1,
text=“子文本”,
儿童=新列表{
新生儿3{
Id=1,
text=“子文本”
},
新生儿3{
Id=2,
text=“子文本”
}
}
},
新生儿2{
Id=2,
text=“子文本”,
儿童=新列表{
新生儿3{
Id=3,
text=“子文本”
},
新生儿3{
Id=4,
text=“子文本”
}
}
}
}
}
},
});
var root = new List<Root>
{
new Root
{
Id = 1,
text = "foo",
children =
{
new Child
{
// ...
},
new Child
{
// ...
},
}
}
};
var root = new List<Root>
{
new Root
{
Id = 1,
text = "foo",
children = new List<Child>
{
new Child
{
// ...
},
new Child
{
// ...
},
}
}
};
var root = new List<Root>();
root.Add(new Root
{
Id = 1,
text = "foo",
children = new List<Child>()
{
new Child{
Id = 1,
text = "child text",
children = new List<Child2>(){
new Child2{
Id = 1,
text = "child text",
children = new List<Child3>{
new Child3{
Id =1,
text = "child text"
},
new Child3{
Id =2,
text = "child text"
}
}
},
new Child2{
Id = 2,
text = "child text",
children = new List<Child3>{
new Child3{
Id =3,
text = "child text"
},
new Child3{
Id =4,
text = "child text"
}
}
}
}
},
//Child 2
new Child{
Id = 1,
text = "child text",
children = new List<Child2>(){
new Child2{
Id = 1,
text = "child text",
children = new List<Child3>{
new Child3{
Id =1,
text = "child text"
},
new Child3{
Id =2,
text = "child text"
}
}
},
new Child2{
Id = 2,
text = "child text",
children = new List<Child3>{
new Child3{
Id =3,
text = "child text"
},
new Child3{
Id =4,
text = "child text"
}
}
}
}
}
},
});