Jonathan Edgardo
Jonathan Edgardo

Reputation: 513

Recursive get all childrens using LINQ C#

i want to show all the parentID as possible using linq for example: if i select "submodule_idparent = 6" return all 5 and return 1. Because, to show 6 i need 5 and 1.

  submodule_id, submodule_name, submodule_idparent

  1             Articles        null
  2             Suppliers       null
  3             Adjustment      1
  4             Presentations   1
  5             Categories      1
  6             Subcategories   5
  7             Corridors       1
  8             Cellars         1
  9             Purchases       2

I'm try some like that with the next code (MySQL)

SELECT DISTINCT(submodule_id) FROM users_privileges LEFT JOIN modules_options USING(moduleoption_id) WHERE users_privileges.user_id = 1

but is not recursive.

Thanks in advance (y).

Upvotes: 0

Views: 360

Answers (1)

Tim.Tang
Tim.Tang

Reputation: 3188

Try this:

var query=GetAll(6);  

public IEnumerable<users_privileges> GetAll(int submodule_id)  
{  
    var query = from c in db.users_privileges 
           where c.submodule_id == submodule_id
           select c;  

    return  query.ToList().Concat(query.ToList().SelectMany(t => GetAll(t.submodule_idparent)));                
}   

http://blog.csdn.net/q107770540/article/details/7708418

Upvotes: 1

Related Questions