为什么谓词在递归函数中调用时会误解参数的值 [英] Why is predicate misinterpreting parameters' values when called in recursive function

查看:85
本文介绍了为什么谓词在递归函数中调用时会误解参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试创建一个继承自现有winform TreeView控件的扩展树视图控件。创建了一个  Load() 在类TreeViewEx中运行
。在此函数中,dataSource在foreach中循环。这个foreach然后调用  Where() 扩展名
循环dataSource上的方法传递给它一个方法(需要作为参数当前元素)返回谓词。该谓词错误地解释了传递给它的参数值。它似乎使用以前的参数值。

I'm trying to create a extended treeview control inheriting from the existing winform TreeView control. Created a Load() function in the class TreeViewEx. In this function the dataSource is looped in a foreach. This foreach then calls the Where() extension method on the looping dataSource passing to it a methode (which takes as parameter the current element) returning a predicate. This predicate misintepretes the parameter value passed to it. It seems to be using previous parameter values.


最初我认为这种行为是由于我正在迭代  Enumerable   not
a list,所以我将不同的枚举更改为List但没有任何改变。还试图实例化返回的谓词,但没有任何内容。

Initially i thought this behavior was due to the fact that i am iterating through an Enumerable not a list, so i change the different enumerables to List but nothing changed. Also tried to instatiate the returned predicate but nothing.


加载函数:

Load function :


public Func<T, Func<T, bool>> GetChildrenPredicate { get; set; }
.
.
.
public virtual void Load(List<T> dataSource = null)
{
    try
    {
        if (CreateNode == null)
        {
            OnError?.Invoke(this, new ArgumentNullException("CreateNode"));
            return;
        }
        if (GetParentKey == null)
        {
            OnError?.Invoke(this, new ArgumentNullException("GetParentKey"));
            return;
        }
        if (GetChildrenPredicate == null)
        {
            OnError?.Invoke(this, new ArgumentNullException("GetChildrenPredicate"));
            return;
        }

        var finalDataSource = dataSource ?? DataSource;

        TreeNode node = null;
        BeginUpdate();
        foreach (var item in finalDataSource)
        {
            node = CreateNode(item);
            node.Tag = item;

            if (this.Nodes.Find(node.Name, true).Count() == 0)
            {
                var n = this.Nodes.Find(this.GetParentKey(item), true).FirstOrDefault() as TreeNode;

                if (n == null)
                {
                    this.Nodes.Add(node);
                }
                else
                {
                    n.Nodes.Add(node);
                }

                List<T> children = finalDataSource
                                  .ToList()                                   
                                  .Where(this.GetChildrenPredicate(item))
                                  .ToList(); //this.GetChildrenPredicate is
                                //the property func generating the 
                                //predicate set by a different class

                if (children.Count() > 0)
                {
                    // Recursively call this function for all childRows
                    Load(children);
                }

            }
        }
        EndUpdate();
    }
    catch (Exception ex)
    {
        OnError?.Invoke(this, ex);
    }
}

GetChildrenPredicate:

private Func<ORM.DataModels.Menu, bool> GetChildrenPredicate(ORM.DataModels.Menu arg)
{

    return (ORM.DataModels.Menu m) =>
    (m.Lepere == arg.Codmen) ||
    (m.Lepere == null && arg.Codmen == "_" + m.Niveau);
}



推荐答案

好的。我找到了解决方案。其实我没有意识到 每次调用`Load()`时都会覆盖`finalDataSource`。我只专注于谓词的奇怪行为。只需使用类中定义的全局DataSource属性。



Ok. I found the solution. Actually i did not realized that  `finalDataSource` was overriden on each call of `Load()`. I was only focused on the weird behaviour of the predicate. just had to used the global DataSource property defined in the class.

List<T> children = this.DataSource.Where(this.GetChildrenPredicate(item)); //<= changed local variable finalDataSource to the defined property this.DataSource








这篇关于为什么谓词在递归函数中调用时会误解参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆