Region_Country递归List的搞法

 System.Data.DataTable dt = SqlHelper.ExecuteDataset("链接字符", System.Data.CommandType.Text, "Select BRC_Name,BRC_Value,BRC_Code,BRC_ParentCode,BRC_Type from BS_Region_Country where BRC_Enabled=1").Tables[0];

//

调用递归

   List<CountryInfo> list = returnList(null, 2, 1);

//递归方法

/// <summary>
/// 递归list集合
/// </summary>
/// <param name="CountryList">递归对象集合,最开始为空</param>
/// <param name="totalCount">总共需要递归多少层</param>
/// <param name="currentNum">当前第几层,初始化时第一层传1</param>
/// <returns></returns>
public List<CountryInfo> returnList(List<CountryInfo> CountryList, int totalCount, int currentNum)
{
if (CountryList == null || CountryList.Count == 0)
{
List<CountryInfo> list = (from t in dt.AsEnumerable() where t["BRC_Type"].ToString() == "GEO" select new CountryInfo() { Brc_Code = t["BRC_Code"].ToString(), Brc_Name = t["BRC_Name"].ToString(), Brc_Value = t["BRC_Value"].ToString(), Brc_ParentCode = t["BRC_ParentCode"].ToString(), Brc_Type = t["BRC_Type"].ToString(), List = new List<CountryInfo>() }).ToList();
CountryList = list;

}
if (currentNum < totalCount)
{
foreach (CountryInfo country in CountryList)
{
var a = from t in dt.AsEnumerable() where GetString(t["BRC_ParentCode"]) == country.Brc_Code select new CountryInfo() { Brc_Code = GetString(t["BRC_Code"]), Brc_Name = GetString(t["BRC_Name"]), Brc_Value = GetString(t["BRC_Value"]), Brc_ParentCode = GetString(t["BRC_ParentCode"]), Brc_Type = GetString(t["BRC_Type"]), List = new List<CountryInfo>() };
if (a.Count() > 0)
{
country.List.AddRange(a);
returnList(country.List, totalCount, currentNum + 1);

}
}
}
return CountryList;
}

//字符串转换

public static string GetString(object obj)
{
return (obj == DBNull.Value || obj == null) ? "" : obj.ToString();
}

//Country对象

public class CountryInfo
{
private string brc_Code;

public string Brc_Code
{
get { return brc_Code; }
set { brc_Code = value; }
}
private string brc_Name;

public string Brc_Name
{
get { return brc_Name; }
set { brc_Name = value; }
}
private string brc_Value;

public string Brc_Value
{
get { return brc_Value; }
set { brc_Value = value; }
}
private string brc_ParentCode;

public string Brc_ParentCode
{
get { return brc_ParentCode; }
set { brc_ParentCode = value; }
}
private string brc_Type;

public string Brc_Type
{
get { return brc_Type; }
set { brc_Type = value; }
}
private List<CountryInfo> list;

public List<CountryInfo> List
{
get { return list; }
set { list = value; }
}
public CountryInfo()
{

}
public CountryInfo(string Brc_Code, string Brc_Name, string Brc_Value, string Brc_ParentCode, string Brc_Type, List<CountryInfo> List = null)
{
this.brc_Code = Brc_Code;
this.brc_Name = Brc_Name;
this.brc_Value = Brc_Value;
this.brc_ParentCode = Brc_ParentCode;
this.brc_Type = Brc_Type;
this.list = List;
}

}

原文地址:https://www.cnblogs.com/liziqiang/p/6734250.html