日期:2014-05-20  浏览次数:20857 次

求一linq查询,得到如下结果
C# code
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IList<Area> list = new List<Area>();
        list.Add(new Area() { Uid=1, Pid=0, Name="电信" });
        list.Add(new Area() { Uid=2, Pid=0, Name="联通" });
        list.Add(new Area() { Uid=3, Pid=0, Name="移动" });
        list.Add(new Area() { Uid=4, Pid=1, Name="电信一区" });
        list.Add(new Area() { Uid=5, Pid=1, Name="电信二区" });
        // 用linq查询得到 uid、name
        // name中显示父级-子级-子子级。。。。,只要最后一级
        // 结果:
        //uid                 name                                        
        //2                    联通                          
        //3                    移动                              
        //4                    电信-电信一区                                 
        //5                    电信-电信二区
    }
}


public class Area
{
    public int Uid { get; set; }
    public int Pid { get; set; }// 父ID
    public string Name { get; set; }
}


------解决方案--------------------
C# code

void Main()
{
     IList<Area> list = new List<Area>();
        list.Add(new Area() { Uid=1, Pid=0, Name="电信" });
        list.Add(new Area() { Uid=2, Pid=0, Name="联通" });
        list.Add(new Area() { Uid=3, Pid=0, Name="移动" });
        list.Add(new Area() { Uid=4, Pid=1, Name="电信一区" });
        list.Add(new Area() { Uid=5, Pid=1, Name="电信二区" });
        
    var query=from  l in list
              select new 
                {
                  Uid=l.Uid,
                  Name=l.Pid==0?l.Name : list.First(m=>m.Uid==l.Pid).Name+"-"+l.Name
                };
     
}
public class Area
{
    public int Uid { get; set; }
    public int Pid { get; set; }// 父ID
    public string Name { get; set; }
}

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Area> list = new List<Area>();
            list.Add(new Area() { Uid = 1, Pid = 0, Name = "电信" });
            list.Add(new Area() { Uid = 2, Pid = 0, Name = "联通" });
            list.Add(new Area() { Uid = 3, Pid = 0, Name = "移动" });
            list.Add(new Area() { Uid = 4, Pid = 1, Name = "电信一区" });
            list.Add(new Area() { Uid = 5, Pid = 1, Name = "电信二区" });
            list.Add(new Area() { Uid = 6, Pid = 5, Name = "电信二区之一" });
            Func<int, int> FindRoot = (x) => x;
            FindRoot = (x) => list.First(y => y.Uid == x).Pid == 0 ? list.First(y => y.Uid == x).Uid : FindRoot(list.First(y => y.Uid == x).Pid);
            var query = from l in list
                        select new
                        {
                            Uid = l.Uid,
                            Name = l.Pid == 0 ? l.Name : list.First(m => m.Uid == FindRoot(l.Uid)).Name + "-" + l.Name
                        };
            foreach (var item in query)
            {
                Console.WriteLine(item.Uid + "," + item.Name);
            }
        }
    }

    class Area
    {
        public int Uid { get; set; }
        public int Pid { get; set; }
        public string Name { get; set; }
    }
}

------解决方案--------------------
C# code

            IList<Area> list = new List<Area>();
            list.Add(new Area() { Uid = 1, Pid = 0, Name = "电信" });
            list.Add(new Area() { Uid = 2, Pid = 0, Name = "联通" });
            list.Add(new Area() { Uid = 3, Pid = 0, Name = "移动" });
            list.Add(new Area() { Uid = 4, Pid = 1, Name = "电信一区" });
            list.Add(new Area() { Uid = 5, Pid = 1, Name = "电信二区" });
            list.Add(new Area() { Uid = 6, Pid = 0, Name = "Test" });
            list.Add(new Area() { Uid = 7, Pid = 6, Name = "Test1" });
            list.Add(new Area() { Uid = 8, Pid = 7, Name = "Test11" });

            Console.WriteLine("uid\t\tname");
            foreach (var area in list.Where(a => !list.Any(l => l.Pid == a.Uid)))
            {
                Console.WriteLine("{0}\t\t{1}", area.Uid, area.Name);
            }

            Console.ReadLine();