public class Organization
{
public string OID { get; set; }
public string FathOID { get; set; }
public string OrganizationDesc { get; set; }
}
public static List<Organization> GetWithChildrenList(List<Organization> list, string organizationID)
{
Organization curr = list.Where(x => x.OID == organizationID).FirstOrDefault();
List<Organization> childrenList = new List<Organization>();
if (curr != null)
{
childrenList = GetChildrenList(list, organizationID);
}
childrenList.Add(curr);
return childrenList;
}
public static List<Organization> GetChildrenList(List<Organization> list, string organizationID)
{
var query = list.Where(x => x.FathOID == organizationID).ToList();
return query.Concat(query.SelectMany(t => GetChildrenList(list, t.OID))).ToList();
}
//测试代码
List<Organization> organizations = new List<Organization>();
Organization ot1 = new Organization(){ OID="001",FathOID="0",OrganizationDesc="公司1"};
Organization ot2 = new Organization() { OID = "002", FathOID = "001", OrganizationDesc = "财务部" };
Organization ot3 = new Organization() { OID = "003", FathOID = "001", OrganizationDesc = "仓储部" };
Organization ot4 = new Organization() { OID = "002001", FathOID = "002", OrganizationDesc = "财务小组21" };
Organization ot5 = new Organization() { OID = "002001001", FathOID = "002001", OrganizationDesc = "财务小组21财务小组21" };
organizations.Add(ot1);
organizations.Add(ot2);
organizations.Add(ot3);
organizations.Add(ot4);
organizations.Add(ot5);
List<Organization> tt = GetWithChildrenList(organizations, "002");