C# 按父关系对项目排序
从一开始,我问自己是否有一种更简单的方法来分类一个像C# 按父关系对项目排序,c#,class,sorting,C#,Class,Sorting,从一开始,我问自己是否有一种更简单的方法来分类一个像 public class ParentChild { int ID { get; set; } int ParentID { get; set; } public ParentChild(int id, int pid) { ID = id; ParentID = pid; } } 取决于它的父关系 e、 g 这个问题不是要按层次顺序显示数据。与我在链接帖子中的回答相
public class ParentChild
{
int ID { get; set; }
int ParentID { get; set; }
public ParentChild(int id, int pid)
{
ID = id;
ParentID = pid;
}
}
取决于它的父关系
e、 g
这个问题不是要按层次顺序显示数据。与我在链接帖子中的回答相比,这只是一个更简单的/非递归的/linq
OrderBy
/Sort
。一旦你在ParentChild
中修复了构造函数,你应该会发现这是可行的:
var lookup = pcItems.ToLookup(x => x.ParentID, x => x.ID);
Func<int, int, IEnumerable<string>> build = null;
build = (pid, level) =>
lookup[pid]
.SelectMany(id =>
new [] { "".PadLeft(level * 4) + id.ToString() }
.Concat(build(id, level + 1)));
IEnumerable<string> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID,x=>x.ID);
Func build=null;
构建=(pid,级别)=>
查找[pid]
.SelectMany(id=>
新[]{.PadLeft(级别*4)+id.ToString()}
.Concat(构建(id,级别+1));
IEnumerable results=build(0,0);
这就给了你:
它是递归的,但至少有三行代码。;-)
要仅获取排序结果,请执行以下操作:
var lookup = pcItems.ToLookup(x => x.ParentID, x => x.ID);
Func<int, int, IEnumerable<ParentChild>> build = null;
build = (pid, level) => lookup[pid]
.SelectMany(id => new[] { pcItems.Single(x => x.ID == id) }
.Concat(build(id, level + 1)));
IEnumerable<ParentChild> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID,x=>x.ID);
Func build=null;
构建=(pid,级别)=>查找[pid]
.SelectMany(id=>new[]{pcItems.Single(x=>x.id==id)}
.Concat(构建(id,级别+1));
IEnumerable results=build(0,0);
稍微干净一点的版本:
var lookup = pcItems.ToLookup(x => x.ParentID);
Func<int, int, IEnumerable<ParentChild>> build = null;
build = (pid, level) =>
lookup[pid].SelectMany(pc => new[] { pc }.Concat(build(pc.ID, level + 1)));
IEnumerable<ParentChild> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID);
Func build=null;
构建=(pid,级别)=>
lookup[pid].SelectMany(pc=>new[]{pc}.Concat(build(pc.ID,level+1));
IEnumerable results=build(0,0);
一旦您在ParentChild
中修复了构造函数,您应该会发现这是可行的:
var lookup = pcItems.ToLookup(x => x.ParentID, x => x.ID);
Func<int, int, IEnumerable<string>> build = null;
build = (pid, level) =>
lookup[pid]
.SelectMany(id =>
new [] { "".PadLeft(level * 4) + id.ToString() }
.Concat(build(id, level + 1)));
IEnumerable<string> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID,x=>x.ID);
Func build=null;
构建=(pid,级别)=>
查找[pid]
.SelectMany(id=>
新[]{.PadLeft(级别*4)+id.ToString()}
.Concat(构建(id,级别+1));
IEnumerable results=build(0,0);
这就给了你:
它是递归的,但至少有三行代码。;-)
要仅获取排序结果,请执行以下操作:
var lookup = pcItems.ToLookup(x => x.ParentID, x => x.ID);
Func<int, int, IEnumerable<ParentChild>> build = null;
build = (pid, level) => lookup[pid]
.SelectMany(id => new[] { pcItems.Single(x => x.ID == id) }
.Concat(build(id, level + 1)));
IEnumerable<ParentChild> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID,x=>x.ID);
Func build=null;
构建=(pid,级别)=>查找[pid]
.SelectMany(id=>new[]{pcItems.Single(x=>x.id==id)}
.Concat(构建(id,级别+1));
IEnumerable results=build(0,0);
稍微干净一点的版本:
var lookup = pcItems.ToLookup(x => x.ParentID);
Func<int, int, IEnumerable<ParentChild>> build = null;
build = (pid, level) =>
lookup[pid].SelectMany(pc => new[] { pc }.Concat(build(pc.ID, level + 1)));
IEnumerable<ParentChild> results = build(0, 0);
var lookup=pcItems.ToLookup(x=>x.ParentID);
Func build=null;
构建=(pid,级别)=>
lookup[pid].SelectMany(pc=>new[]{pc}.Concat(build(pc.ID,level+1));
IEnumerable results=build(0,0);
“我在我的链接帖子中有一个工作方法,所以不需要要求efford”——是的,有。每个堆栈溢出问题都需要独立处理,因此,如果相关问题被删除,那么这个问题仍然有意义。“我在链接的帖子中有一个工作方法,所以不需要请求efford”——是的,有。每个堆栈溢出问题都需要独立处理,这样当相关问题被删除时,该问题仍然有意义。@fubo-您刚刚在树输出中添加了行。那有点繁重。忘了台词吧,它只是sorting@fubo-够远了。我想你会发现你需要递归来获得正确的排序顺序。我在你的答案中添加了我的修改-请随意滚动back@fubo-我在您的下面添加了一个稍微干净的代码版本。@fubo-您刚刚在树输出中添加了行。那有点繁重。忘了台词吧,它只是sorting@fubo-够远了。我想你会发现你需要递归来获得正确的排序顺序。我在你的答案中添加了我的修改-请随意滚动back@fubo-我在你的代码下添加了一个稍微干净的版本。