【发布时间】:2011-01-27 10:11:42
【问题描述】:
我有以下方法使用 LinqToSql 获取节点的所有父节点,但我不知道这对性能有多大影响。
来自节点表:
public partial class Node
{
public List<Node> GetAllParents(IEnumerable<Node> records)
{
if (this.ParentID == 0)
{
// Reach the parent, so create the instance of the collection and brake recursive.
return new List<Node>();
}
var parent = records.First(p => p.ID == ParentID);
// create a collection from one item to concat it with the all parents.
IEnumerable<Node> lst = new Node[] { parent };
lst = lst.Concat(parent.GetAllParents(records));
return lst.ToList();
}
}
这样好吗!!或任何改进它的想法!
谢谢。
【问题讨论】:
-
@Ani:不,它在由 LinqToSQL 创建的生成的部分类中。
-
你不应该担心性能,除非你发现它在执行时确实需要太多时间。这时候你可以看看你最常松懈的地方。
标签: c# linq performance linq-to-sql