c#嵌套对象到分层对象列表
我有一个c类型来生成层子层c#嵌套对象到分层对象列表,c#,linq,hierarchical-data,C#,Linq,Hierarchical Data,我有一个c类型来生成层子层 public class Layer { public int Id { get; set; } public int ParentId { get; set; } public string Name { get; set; } } 并用Db类模拟了一个列表 public static class Db { public static IList<Layer> GetLayers() {
public class Layer
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
}
并用Db类模拟了一个列表
public static class Db
{
public static IList<Layer> GetLayers()
{
return new List<Layer>
{
new Layer{Id = 1, ParentId = 0, Name = "First Layer" },
new Layer{Id = 2, ParentId = 1, Name = "First SubLayer1" },
new Layer{Id = 3, ParentId = 1, Name = "First SubLayer2" },
new Layer{Id = 4, ParentId = 1, Name = "First SubLayer3" },
new Layer{Id = 5, ParentId = 0, Name = "Second Layer" },
new Layer{Id = 6, ParentId = 5, Name = "Second SubLayer1" },
new Layer{Id = 7, ParentId = 5, Name = "Second SubLayer2" },
new Layer{Id = 8, ParentId = 7, Name = "Sub -3" }
};
}
}
公共静态类数据库
{
公共静态IList GetLayers()
{
返回新列表
{
新层{Id=1,ParentId=0,Name=“First Layer”},
新层{Id=2,ParentId=1,Name=“First SubLayer1”},
新层{Id=3,ParentId=1,Name=“First SubLayer2”},
新层{Id=4,ParentId=1,Name=“First SubLayer3”},
新层{Id=5,ParentId=0,Name=“第二层”},
新层{Id=6,ParentId=5,Name=“Second SubLayer1”},
新层{Id=7,ParentId=5,Name=“Second SubLayer2”},
新层{Id=8,ParentId=7,Name=“Sub-3”}
};
}
}
所以我想得到一个序列化的Json对象。我创建了一个服务类来生成
和更改的图层类别:
public class Layer
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public IList<Layer> ChildLayers { get; set; }
public Layer()
{
ChildLayers = new List<Layer>();
}
}
public class LayerService
{
public IList<Layer> GetLayers()
{
IList<Layer> data = Db.GetLayers();
IList<Layer> hierarcy = new List<Layer>();
foreach (var layer in data)
{
var layer1 = layer;
var sublayers = data.Where(i => i.ParentId == layer1.Id && i.ParentId !=0);
var enumerable = sublayers as Layer[] ?? sublayers.ToArray();
if(enumerable.Any() && layer.ParentId ==0)
hierarcy.Add(layer);
foreach (var sublayer in enumerable)
{
layer.ChildLayers.Add(sublayer);
}
}
return hierarcy;
}
}
公共类层
{
公共int Id{get;set;}
public int ParentId{get;set;}
公共字符串名称{get;set;}
公共IList子层{get;set;}
公共层()
{
ChildLayers=新列表();
}
}
公共类分层服务
{
公共IList GetLayers()
{
IList data=Db.GetLayers();
IList hierarcy=新列表();
foreach(数据中的var层)
{
var layer1=层;
var sublayers=data.Where(i=>i.ParentId==layer1.Id&&i.ParentId!=0);
var enumerable=子层作为层[]??子层。ToArray();
if(enumerable.Any()&&layer.ParentId==0)
层次结构。添加(层);
foreach(可枚举中的var子层)
{
layer.ChildLayers.Add(子层);
}
}
回归等级制;
}
}
但这层服务代码很难看。linq或其他人有什么切实可行的方法吗?干净的代码方式?如果您想使用LINQ,下面的代码可能会帮助您:
public IList<Layer> GetLayers()
{
IList<Layer> data = Db.GetLayers();
IList<Layer> hierarcy = new List<Layer>();
data.Where(x => x.ParentId == 0).ToList().ForEach(x => hierarcy.Add(x)); //get parent
data.Where(a => a.ParentId != 0).ToList().
ForEach(a =>
{
hierarcy.Where(b => b.Id == a.ParentId).ToList().ForEach(c => c.ChildLayers.Add(a)); //get childrens
});
return hierarcy;
}
public IList GetLayers()
{
IList data=Db.GetLayers();
IList hierarcy=新列表();
data.Where(x=>x.ParentId==0).ToList().ForEach(x=>hierarcy.Add(x));//获取父对象
其中(a=>a.ParentId!=0).ToList()。
ForEach(a=>
{
hierarcy.Where(b=>b.Id==a.ParentId).ToList().ForEach(c=>c.ChildLayers.Add(a));//获取childrens
});
回归等级制;
}
如果您想与LINQ合作,下面的代码可能会对您有所帮助:
public IList<Layer> GetLayers()
{
IList<Layer> data = Db.GetLayers();
IList<Layer> hierarcy = new List<Layer>();
data.Where(x => x.ParentId == 0).ToList().ForEach(x => hierarcy.Add(x)); //get parent
data.Where(a => a.ParentId != 0).ToList().
ForEach(a =>
{
hierarcy.Where(b => b.Id == a.ParentId).ToList().ForEach(c => c.ChildLayers.Add(a)); //get childrens
});
return hierarcy;
}
public IList GetLayers()
{
IList data=Db.GetLayers();
IList hierarcy=新列表();
data.Where(x=>x.ParentId==0).ToList().ForEach(x=>hierarcy.Add(x));//获取父对象
其中(a=>a.ParentId!=0).ToList()。
ForEach(a=>
{
hierarcy.Where(b=>b.Id==a.ParentId).ToList().ForEach(c=>c.ChildLayers.Add(a));//获取childrens
});
回归等级制;
}
如果您的代码正常工作,为什么要将var enumerable=子层作为层[]??子层。ToArray()代码>当sublayers
无论如何都不会成为数组时?问题现在被交叉贴出来了,如果您的代码有效,为什么要将var enumerable=sublayers作为层[]??子层。ToArray()代码>当子层
无论如何永远不会是数组时?问题现在被交叉张贴