我試圖在.NET 2.0(沒錯,2.0)的應用程序來填充分層數據而現在升級爲假表(所以沒有LINQ,LINQ橋,或其他東西)。有沒有更好的方法來將SQL中的分層數據填充到類結構,C#或VB.NET中?
我不知道是否有更好的方法來填充分層數據到這個階層結構?我很確定有一個更好的方法來完成這個任務。
這將是非常高興看到一個好辦法做到這一點。如果任何人有時間展示.NET 2.0的方式,並且如果有其他方式,他們會在.NET 4.0+中做到這一點,那將非常棒。
這裏是節點類型結構的一個例子:
using System.Collections.Generic;
public class ExampleNode
{
private int _id;
private Nullable<int> _parentId;
private int _depth;
private List<ExampleNode> _children = new List<ExampleNode>();
public ExampleNode()
{
}
public virtual int ApplicationNumber {
get { return _id; }
set { _id = value; }
}
public virtual Nullable<int> ParentId {
get { return _parentId; }
set { _parentId = value; }
}
public virtual int Depth {
get { return _depth; }
set { _depth = value; }
}
public virtual List<ExampleNode> Children {
get { return _children; }
set { _children = value; }
}
}
這裏是正被使用來填充所述節點結構的一例的功能。看來這不是實現這一目標的最佳方式,它有可能不會填充孫子類型數據。 Depth從存儲過程中返回,作爲層級中的級別(0級別的項目是頂級,如果某個節點是頂級節點的子級,則它位於級別1,頂級節點的孫級是級別2等)
public List<ExampleNode> GetNodes()
{
// This may not be optimal.
List<ExampleNode> nodeList = new List<ExampleNode>();
Dictionary<int, ExampleNode> nodeDictionary = new Dictionary<int, ExampleNode>();
using (SqlDataReader reader = SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings("SqlServer").ConnectionString, CommandType.StoredProcedure, "proc_GetNodeStructure", new SqlParameter("@UserId", userId), new SqlParameter("@NodeTypeId", nodeType))) {
while (reader.Read) {
ExampleNode nodeInstance = new ExampleNode();
nodeInstance.Id = Convert.ToInt32(reader("Id"));
nodeInstance.Depth = Convert.ToInt32(reader("Depth"));
if (!Information.IsDBNull(reader("ParentId"))) {
nodeInstance.ParentId = Convert.ToInt64(reader("ParentId"));
}
// Add to list
nodeList.Add(nodeInstance);
// Add to dictionary
nodeDictionary.Add(nodeInstance.Id, nodeInstance);
}
}
foreach (ExampleNode item in nodeList) {
if (item.ParentId.HasValue) {
nodeDictionary(item.ParentId).Children.Add(item);
}
}
for (int i = nodeList.Count - 1; i >= 0; i += -1) {
if (nodeList(i).Depth > 0) {
nodeList.RemoveAt(i);
}
}
return nodeList;
}
我不知道很多人會考慮轉向LINQ進行「升級」。 – 2012-07-09 02:12:19
您是否必須使用數據讀取器?我在想,如果你可以使用數據表並在遞歸函數中使用它,首先獲得根節點('ParentId IS NULL'),然後根據父id值('ParentId = ')等等。使用'LINQ'你有更多的靈活性,但是如果你被限制使用'.NET 2.0',你就不能使用它。 –
2012-07-09 02:12:29