所以我有一堆 UltraTreeNodes。我正在尝试遍历它们并将 LINQ 查询分配给 Tag 属性,该属性将在稍后用户单击节点时执行。对每个节点的查询唯一改变的是 where 子句中的变量。但是,当单击节点并最终从标记执行查询时,它始终使用在 foreach 循环中最后分配的查询(并且是 where 变量)。
它的行为是否因为所有标签都引用相同的查询,因此变量被一遍又一遍地分配给相同的地方?有没有办法避免这种行为而不必动态创建谓词并将其传递?
更新- 对不起,这里是代码:
var query = from result in results
where result.SchedulingStatus != SchedulerRunStatus.Scheduled
group result by result.UserId into users
select new {users.Key, Count = users.Count()};
foreach (var user in query)
{
UltraTreeNode efdUserNode = new UltraTreeNode();
efdUserNode.Text = user.Key + " (" + user.Count + ")";
efdUserNode.Key = user.Key;
IEnumerable<DownloadResultSummary> nodeQuery = from result in efdDetail.DownloadResultSummaries
where
result.SchedulingStatus != SchedulerRunStatus.Scheduled &
result.UserId == user.Key
select result;
efdUserNode.Tag = nodeQuery;
efdUserNode.Override.NodeAppearance.Image = Properties.Resources.user;
this.efdNode.Nodes.Add(efdUserNode);
}