Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何遍历classA中的classA列表?_C#_List - Fatal编程技术网

C# 如何遍历classA中的classA列表?

C# 如何遍历classA中的classA列表?,c#,list,C#,List,我有一个叫做FilterType的类。在这个类中,我有一个与数据成员相同的类的列表。我试图循环浏览列表中的每个成员和列表中的列表。这是我的密码 public class FilterType { string name; int id; string OR;// Either "OR" or AND only one condition is true. string AND;// List<FilterType> fl = new List&

我有一个叫做FilterType的类。在这个类中,我有一个与数据成员相同的类的列表。我试图循环浏览列表中的每个成员和列表中的列表。这是我的密码

public class FilterType
{
    string name;
    int id;
    string OR;// Either "OR" or AND only one condition is true.
    string AND;//
    List<FilterType> fl = new List<FilterType>();
}

我不知道如何浏览这个列表中的每个成员。如果有人有任何建议,请告诉我。

您需要的是循环,您可以使用C语言的任何循环元素:

foreach(fl中的var项目) { Console.WriteLine(item.GetType()); }

或使用索引进行迭代的表达式

对于(变量i=0;i 希望这有帮助


PS:很抱歉在我的手机上进行编辑和写作

您需要的是循环,您可以使用C语言的任何循环元素:

foreach(fl中的var项目) { Console.WriteLine(item.GetType()); }

或使用索引进行迭代的表达式

对于(变量i=0;i 希望这有帮助


PS:很抱歉在我的手机上编辑、编写

如果你想递归枚举,你可以使用
yield return
来完成

public IEnumerable<FilterType> EnumerateRecursively()
{
    yield return this;

    foreach (var item in fl)
    {
        foreach (var subItem in item.EnumerateRecursively())
        {
            yield return subItem;
        }
    }
}
public IEnumerable递归枚举()
{
收益回报这一点;
foreach(fl中的var项目)
{
foreach(item.Enumeratively()中的var子项)
{
收益回报子项;
}
}
}

C#在不进行中间累加和不必编写大量样板代码的情况下,使其变得相对简单。

如果要递归枚举,可以使用
yield return
进行枚举

public IEnumerable<FilterType> EnumerateRecursively()
{
    yield return this;

    foreach (var item in fl)
    {
        foreach (var subItem in item.EnumerateRecursively())
        {
            yield return subItem;
        }
    }
}
public IEnumerable递归枚举()
{
收益回报这一点;
foreach(fl中的var项目)
{
foreach(item.Enumeratively()中的var子项)
{
收益回报子项;
}
}
}

C#在没有中间累加和不需要编写大量样板代码的情况下,这样做相对简单。

我不明白您到底想要实现什么

如果您只想循环列表fl,有很多方法可以实现

我会像这样使用foreach循环

foreach (var item in fl)
{
   // Do something with item.
}
if(filterType.IsEmpty())
{
   // Do something.
}
如果你想在课堂外循环列表

而不是将列表公开

建议您在类内创建一个函数,该函数使用类外的“列表”执行某些操作

我是说,如果你想在课外处理这张单子

if(FilterType.fl.Count > 0)
{
   // Do something.
}
public class FilterType
{
   string name;
   int id;
   List<FilterType> fl = new List<FilterType>();

   public bool IsEmpty()
   {
      return fl.Count > 0 ? false : true;
   }
}
我建议您在类中创建这样的函数

if(FilterType.fl.Count > 0)
{
   // Do something.
}
public class FilterType
{
   string name;
   int id;
   List<FilterType> fl = new List<FilterType>();

   public bool IsEmpty()
   {
      return fl.Count > 0 ? false : true;
   }
}
我希望这个答案能对你有所帮助


谢谢你。

我不明白你到底想要实现什么

如果您只想循环列表fl,有很多方法可以实现

我会像这样使用foreach循环

foreach (var item in fl)
{
   // Do something with item.
}
if(filterType.IsEmpty())
{
   // Do something.
}
如果你想在课堂外循环列表

而不是将列表公开

建议您在类内创建一个函数,该函数使用类外的“列表”执行某些操作

我是说,如果你想在课外处理这张单子

if(FilterType.fl.Count > 0)
{
   // Do something.
}
public class FilterType
{
   string name;
   int id;
   List<FilterType> fl = new List<FilterType>();

   public bool IsEmpty()
   {
      return fl.Count > 0 ? false : true;
   }
}
我建议您在类中创建这样的函数

if(FilterType.fl.Count > 0)
{
   // Do something.
}
public class FilterType
{
   string name;
   int id;
   List<FilterType> fl = new List<FilterType>();

   public bool IsEmpty()
   {
      return fl.Count > 0 ? false : true;
   }
}
我希望这个答案能对你有所帮助


谢谢。

基本上,您需要的是递归迭代。许多数据结构(树)都需要类似的功能。您可以使用循环执行此操作:

        private static void Recurse(List<FilterType> fl)
        {
            foreach (var item in fl)
            {
                //do whatever you want with the current item, am just setting the name again
                item.name = "Looped through";

                //Go through the child list
                Recurse(item.fl);
            }
        }

一定要小心递归,如果您的数据很大,那么代码可能无法执行,并且有可能抛出内存不足异常。

基本上,您需要的是递归迭代。许多数据结构(树)都需要类似的功能。您可以使用循环执行此操作:

        private static void Recurse(List<FilterType> fl)
        {
            foreach (var item in fl)
            {
                //do whatever you want with the current item, am just setting the name again
                item.name = "Looped through";

                //Go through the child list
                Recurse(item.fl);
            }
        }
public enum LogicalOperator { OR, AND }

public class FilterType
{
    public string name {get;set;}
    public int id {get;set}
    public LogicalOperator ConditionType {get;set;}

    public List<FilterType> fl {get;private set;} = new List<FilterType>();

    public IEnumerable<FilterType> Descendants {
        get 
        {
            foreach(var f in fl)
            {
                yield return f;
                foreach(var child in f.Descendants)
                {
                    yield return child;
                }
            }
        }
    }  
}
一定要小心递归,如果您的数据很大,那么代码可能无法执行,并且有可能抛出内存不足异常。

public enum LogicalOperator{OR,and}
public enum LogicalOperator { OR, AND }

public class FilterType
{
    public string name {get;set;}
    public int id {get;set}
    public LogicalOperator ConditionType {get;set;}

    public List<FilterType> fl {get;private set;} = new List<FilterType>();

    public IEnumerable<FilterType> Descendants {
        get 
        {
            foreach(var f in fl)
            {
                yield return f;
                foreach(var child in f.Descendants)
                {
                    yield return child;
                }
            }
        }
    }  
}
公共类筛选器类型 { 公共字符串名称{get;set;} 公共int id{get;set} 公共逻辑运算符条件类型{get;set;} public List fl{get;private set;}=new List(); 公共数字后裔{ 得到 { foreach(在fl中的变量f) { 收益率f; foreach(f.substands中的变量child) { 退换子女; } } } } }
公共枚举逻辑运算符{OR,AND}
公共类筛选器类型
{
公共字符串名称{get;set;}
公共int id{get;set}
公共逻辑运算符条件类型{get;set;}
public List fl{get;private set;}=new List();
公共数字后裔{
得到
{
foreach(在fl中的变量f)
{
收益率f;
foreach(f.substands中的变量child)
{
退换子女;
}
}
}
}  
}

谢谢您的回复。上面的代码用于gerate或filter类型。(很抱歉,我在完成键入之前意外地点击了“enter”),然后我遍历列表并生成一个数学表达式。感谢您的回复。上面的代码用于gerate或filter类型(很抱歉,我在完成键入之前意外地点击了“enter”),然后遍历列表并生成一个数学表达式。这种递归结构有多深?使用FT for FilterType,您有一个包含FT列表的FT,每个FT都有一个FT列表,依此类推。这是一个简单的
树结构
。如果你研究如何穿越或行走一棵树,你应该会找到很多解决方案。是的,你说得对。这是一个递归结构。递归循环将一直运行,直到找到ft.count=0。例如:如果我们有L(比如根),在L内部,我们有L1、L2、L3等等。在L1内部,我们有L11,L12,