递归方法实现如下图:c# 或者LINQ 实现都可。

一叶知秋う 2016-08-30 10:36:48
集合:
Id Name Parent ParentName
001 李四 000 张三
002 王五 001 李四
003 赵六 002 王五
004 孙七 003 赵六
101 小二 100 小一
102 小三 101 小二
201 红太郎 200 灰太狼


结果:
004 孙七 000 张三
102 小三 100 小一
201 红太郎 200 灰太狼
...全文
368 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2016-08-31
  • 打赏
  • 举报
回复
004 孙七 000 张三 102 小三 100 小一 201 红太郎 200 灰太狼 Press any key to continue . . .
threenewbee 2016-08-31
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> s = @"001 李四 000 张三
002 王五 001 李四
003 赵六 002 王五
004 孙七 003 赵六
101 小二 100 小一
102 小三 101 小二
201 红太郎 200 灰太狼".Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            List<List<string>> list = new List<List<string>>();
            while (s.Count > 0)
            { 
                List<string> sublist = new List<string>();
                sublist.Add(s[0]);
                s.Remove(s[0]);
                string s1 = null, s2 = null;
                do
                {
                    s1 = s.FirstOrDefault(x => x.Split(' ')[0] == sublist.First().Split(' ')[2]);
                    if (s1 != null) { sublist = new string[] { s1 }.Concat(sublist).ToList(); s.Remove(s1); }
                    s2 = s.FirstOrDefault(x => x.Split(' ')[2] == sublist.Last().Split(' ')[0]);
                    if (s2 != null) { sublist.Add(s2); s.Remove(s2); }
                }
                while (s1 != null || s2 != null);
                list.Add(sublist);
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]);
            }
        }
    }
}
threenewbee 2016-08-31
  • 打赏
  • 举报
回复
引用 3 楼 jh_woif 的回复:
[quote=引用 1 楼 caozhy 的回复:]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> s = @"001 李四 000 张三
002 王五 001 李四
003 赵六 002 王五
004 孙七 003 赵六
101 小二 100 小一
102 小三 101 小二
201 红太郎 200 灰太狼".Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            List<List<string>> list = new List<List<string>>();
            while (s.Count > 0)
            { 
                List<string> sublist = new List<string>();
                sublist.Add(s[0]);
                s.Remove(s[0]);
                string s1 = null, s2 = null;
                do
                {
                    s1 = s.FirstOrDefault(x => x.Split(' ')[0] == sublist.First().Split(' ')[2]);
                    if (s1 != null) { sublist = new string[] { s1 }.Concat(sublist).ToList(); s.Remove(s1); }
                    s2 = s.FirstOrDefault(x => x.Split(' ')[2] == sublist.Last().Split(' ')[0]);
                    if (s2 != null) { sublist.Add(s2); s.Remove(s2); }
                }
                while (s1 != null || s2 != null);
                list.Add(sublist);
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]);
            }
        }
    }
}
foreach (var item in list) { Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]); } 牛!你这是数据逻辑排序实现的结果,这里的list 能不能是 {004 孙七 000 张三 102 小三 100 小一 201 红太郎 200 灰太狼} 的集合[/quote] 什么逻辑排序?我没有排序
  • 打赏
  • 举报
回复
通过Node节点这种方式,建立完格式后,一直找child就行了
一叶知秋う 2016-08-31
  • 打赏
  • 举报
回复
 var data = from epf in db.EntProofInfoList
                       join esr in db.EntSellRelation on epf.Relation equals esr.Relation
                       where epf.MainID == MainID && esr.IsDel == false && epf.IsDel == false 
                       select esr;

            var dataList = data.Where(o => o.BuyerEntMainID == MainID);
            if (dataList .Count() != 0)
            {
                foreach (var item in dataList)
                {
                    var dataValue = data.Where(o => o.BuyerID == item.SellerID);
                    if (dataValue.Count() != 0)
                    {
                        item.SellerEntName = dataValue.FirstOrDefault().SellerEntName;
                        item.SellerID = dataValue.FirstOrDefault().BuyerID;
                        item.RelationType = dataValue.FirstOrDefault().RelationType;
                    }
                }
            
            }
一叶知秋う 2016-08-31
  • 打赏
  • 举报
回复
   var dataList = data.Where(o => o.BuyerEntMainID == MainID);
            if (dataList .Count() != 0)
            {
                foreach (var item in dataList)
                {
                    var dataValue = data.Where(o => o.BuyerID == item.SellerID);
                    if (dataValue.Count() != 0)
                    {
                        item.SellerEntName = dataValue.FirstOrDefault().SellerEntName;
                        item.SellerID = dataValue.FirstOrDefault().BuyerID;
                        item.RelationType = dataValue.FirstOrDefault().RelationType;
                    }
                }
            
            }
部分代码是这样的,只能挖到第二层,不能进一步向下挖...
一叶知秋う 2016-08-31
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> s = @"001 李四 000 张三
002 王五 001 李四
003 赵六 002 王五
004 孙七 003 赵六
101 小二 100 小一
102 小三 101 小二
201 红太郎 200 灰太狼".Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            List<List<string>> list = new List<List<string>>();
            while (s.Count > 0)
            { 
                List<string> sublist = new List<string>();
                sublist.Add(s[0]);
                s.Remove(s[0]);
                string s1 = null, s2 = null;
                do
                {
                    s1 = s.FirstOrDefault(x => x.Split(' ')[0] == sublist.First().Split(' ')[2]);
                    if (s1 != null) { sublist = new string[] { s1 }.Concat(sublist).ToList(); s.Remove(s1); }
                    s2 = s.FirstOrDefault(x => x.Split(' ')[2] == sublist.Last().Split(' ')[0]);
                    if (s2 != null) { sublist.Add(s2); s.Remove(s2); }
                }
                while (s1 != null || s2 != null);
                list.Add(sublist);
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]);
            }
        }
    }
}
foreach (var item in list) { Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]); } 牛!你这是数据逻辑排序实现的结果,这里的list 能不能是 {004 孙七 000 张三 102 小三 100 小一 201 红太郎 200 灰太狼} 的集合
一叶知秋う 2016-08-31
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> s = @"001 李四 000 张三
002 王五 001 李四
003 赵六 002 王五
004 孙七 003 赵六
101 小二 100 小一
102 小三 101 小二
201 红太郎 200 灰太狼".Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
            List<List<string>> list = new List<List<string>>();
            while (s.Count > 0)
            { 
                List<string> sublist = new List<string>();
                sublist.Add(s[0]);
                s.Remove(s[0]);
                string s1 = null, s2 = null;
                do
                {
                    s1 = s.FirstOrDefault(x => x.Split(' ')[0] == sublist.First().Split(' ')[2]);
                    if (s1 != null) { sublist = new string[] { s1 }.Concat(sublist).ToList(); s.Remove(s1); }
                    s2 = s.FirstOrDefault(x => x.Split(' ')[2] == sublist.Last().Split(' ')[0]);
                    if (s2 != null) { sublist.Add(s2); s.Remove(s2); }
                }
                while (s1 != null || s2 != null);
                list.Add(sublist);
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Last().Split(' ')[0] + " " + item.Last().Split(' ')[1] + " " + item.First().Split(' ')[2] + " " + item.First().Split(' ')[3]);
            }
        }
    }
}
结贴了,谢谢。你处理问题的逻辑对我有用!

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧