繁体   English   中英

如何在具有与属性相同的列表的类中遍历?

[英]How to traverse within a class that has list of the same as property?

我有这样的课

 public class FilterConditionDto
{
    public string Type { get; set; }

    public bool Parent { get; set; }

    public string[] Value { get; set; }

    public string Column { get; set; }

    public string DataType { get; set; }

    public string Operator { get; set; }

    public List<FilterConditionDto> nodes { get; set; }
}

我如何创建一个遍历内部列表的所有元素和内部列表元素的循环,请注意,列表中的元素也可以在内部包含一个列表。

您可以使用递归来做您想做的事情:

private void Filter(FilterConditionDto dto)
{
    if (dto != null)
    {
        // do something with dto

        if (dto.nodes != null)
        {
            foreach (FilterConditionDto subDto in dto.nodes)
            {
                Filter(subDto); // <-- here you call the method recursively
            }
        }
    }
}

递归

public void Traverse(List<FilterConditionDto> models)
{
    foreach(var item in models)
    {
      if(item.nodes.Count > 0)
      {
          Traverse(item.nodes)
      }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM