【轉】C#遞迴查詢

來源:互聯網
上載者:User

標籤:語句   name   user   tmp   sele   linq   com   pre   www.   

轉自:http://www.cnblogs.com/no27/p/6673893.html

一、sql

--構造測試資料: 只作示範用 CREATE TABLE [dbo].[Tim_LinqTable]( [Id] int PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NOT NULL, [Parent] int NOT NULL, ) GO    INSERT INTO [Tim_LinqTable]   SELECT ‘A‘,0 UNION ALL SELECT ‘A1‘,1 UNION ALL SELECT ‘A2‘,1 UNION ALL SELECT ‘B1‘,2 UNION ALL SELECT ‘B2‘,3 UNION ALL SELECT ‘C1‘,4 UNION ALL SELECT ‘C2‘,4 UNION ALL SELECT ‘D1‘,5 UNION ALL SELECT ‘D2‘,5 UNION ALL SELECT ‘D3‘,5  GO    WITH temp AS ( SELECT * FROM [Tim_LinqTable]  WHERE Parent = 3 UNION ALL SELECT m.* FROM [Tim_LinqTable]  AS m INNER JOIN temp AS child ON m.Parent = child.Id ) SELECT * FROM temp GO    --查詢 Parent=3 的所有子資料結果如下: Id          Name                                               Parent ----------- -------------------------------------------------- ----------- 5           B2                                                 3 8           D1                                                 5 9           D2                                                 5 10          D3                                                 5    (4 row(s) affected) 

二、實現

//好,下邊來看看用C#怎麼實現上邊的SQL語句吧: void Main() {      var query=GetSonID(3);      Console.WriteLine("Id\tName\tParent");      query.ToList().ForEach(q=>Console.WriteLine("{0}\t{1}\t{2}",q.Id,q.Name,q.Parent));      /*        Id       Name       Parent        5        B2         3        8        D1         5        9        D2         5            10        D3         5     */ } public IEnumerable<Tim_LinqTable> GetSonID(int p_id) {     var query = from c in this.Tim_LinqTables            where c.Parent  == p_id            select c;                 return  query.ToList().Concat(query.ToList().SelectMany(t => GetSonID(t.Id)));               } 

或者:

public static List<db_userinfo> findallchildren(int parentid){    var list = (from c in ctdt.db_userinfo                where c.parent_id == parentid                select c).ToList();    List<db_userinfo> tmpList = new List<db_userinfo>(list);    foreach (db_userinfo single in temp)    {        List<db_userinfo> tmpChildren = findallchildren(single.user_id);        if (single.PB_Level == 1)//如果迴圈到最後一層退出        {            return list;        }        if (tmpChildren.Count != 0)        {            list.AddRange(tmpChildren);        }    }    return list;}

 

【轉】C#遞迴查詢

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.