C# 这个for循环多次循环相同的值?

C# 这个for循环多次循环相同的值?,c#,for-loop,C#,For Loop,为什么for循环多次通过相同的值?我在代码中添加了categories类,这样您就可以看到我的意思,它们是实际列表。Theese列表保留了被置乱的值,并且应该根据它们是否具有相同的三个第一个数字来解读这些值 for(int i = 0; i < categories2.parentCategoryId.Count; i++) { for (int j = 0; j < categories2.NewCategoryId.Count; j++)

为什么for循环多次通过相同的值?我在代码中添加了categories类,这样您就可以看到我的意思,它们是实际列表。Theese列表保留了被置乱的值,并且应该根据它们是否具有相同的三个第一个数字来解读这些值

     for(int i = 0; i < categories2.parentCategoryId.Count; i++)
     {
        for (int j = 0; j < categories2.NewCategoryId.Count; j++)
        {
            if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[j].Substring(0, 3)){
                   
                Console.Write(categories2.parentCategoryId[i] + " ");
                Console.Write(categories2.parentCategoryName[i] + "´          ");
                Console.Write(categories2.NewCategoryId[j] + " ");
                Console.Write(categories2.NewCategoryName[j] + "\n"); 

            }
        }
     }




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrestaConverter
{
    public class Categories
    {
        public List<string> CategoryName = new List<string>();
        public List<string> CategoryId = new List<string>();

        public List<string> NewCategoryId = new List<string>();
        public List<string> NewCategoryName = new List<string>();

        public List<string> oldCategoryName = new List<string>();
        public List<string> oldCategoryId = new List<string>();
        
        public List<string> parentCategoryId = new List<string>();
        public List<string> parentCategoryName = new List<string>();

        public List<string> childCategoryID = new List<string>();
        public List<string> childCategoryName = new List<string>();

        public Categories()
        {
          

        } 
        public bool checkFirstNumbers(string categoryid,string correctNumber)
        {
            for(int i = 0; i < 2; i++){
                if(categoryid.Substring(i) == correctNumber.Substring(i))
                {
                    return true; 
                }
            }
           
            
            
            return false; 

        }

    }
}
for(int i=0;i

好的,这是完整的解释,我有3个类别,其中3个对象保留了6个不同的列表。类别2是一系列子类别的列表,应该存在于类别1之下。因此,这些类别有两种不同的标识方式,即id或名称,因此我们有NewCategoryId和NewCategoryName。这些类别列表中没有两个相同的值。但是,我们有ParencegoryName和parentCategoryId列表,theese保留ParentCategoryName的次数,以使其能够将子类别置于em之下。现在,我们需要将NewCategoryID与parentCategoryId进行比较,以查看在Excel文档中放置NewCategoryName的位置。这是完整的故事

来解释一下,正如我在评论中提到的,如果
分类2.parentCategoryId.Count
分类2.NewCategoryId.Count
都相同,那么示例代码可能如下所示:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
        if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[i].Substring(0, 3)){

            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(categories2.NewCategoryId[i] + " ");
            Console.Write(categories2.NewCategoryName[i] + "\n"); 
    }
 }
for(int i = 0; i < 10; i++)
{
    // this is run 10 times
    for(int j = 0; j < 10; j++)
    {
       // this runs 10 times for each iteration of i, so all in all 100 times
    }
}
for(int i=0;i
您的代码使用嵌套循环,其工作方式如下:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
        if(categories2.parentCategoryId[i].Substring(0, 3) == categories2.NewCategoryId[i].Substring(0, 3)){

            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(categories2.NewCategoryId[i] + " ");
            Console.Write(categories2.NewCategoryName[i] + "\n"); 
    }
 }
for(int i = 0; i < 10; i++)
{
    // this is run 10 times
    for(int j = 0; j < 10; j++)
    {
       // this runs 10 times for each iteration of i, so all in all 100 times
    }
}
for(int i=0;i<10;i++)
{
//这是运行10次
对于(int j=0;j<10;j++)
{
//对于i的每个迭代,它运行10次,因此总共运行100次
}
}
如果我正确理解了您需要的功能,并且您使用了
List
,您可以使用以下内容:

 for(int i = 0; i < categories2.parentCategoryId.Count; i++)
 {
     var match = categories2.NewCategoryId.FirstOrDefault(stringToCheck => stringToCheck.Substring(0, 3) == categories2.parentCategoryId[i].Substring(0, 3)));
    if(match != null)
    {
            Console.Write(categories2.parentCategoryId[i] + " ");
            Console.Write(categories2.parentCategoryName[i] + "´          ");
            Console.Write(match  + " ");
            Console.Write(match  + "\n"); 
    }
 }
for(int i=0;istringToCheck.Substring(0,3)==categories2.parentCategoryId[i]。子字符串(0,3));
如果(匹配!=null)
{
Console.Write(categories2.parentCategoryId[i]+“”);
Console.Write(categories2.parentCategoryName[i]+“');
控制台。写入(匹配+“”);
控制台。写入(匹配+“\n”);
}
}
这里应该发生的是,循环在第一个列表中运行,并且在每次迭代中检查第二个列表中是否存在具有相同子字符串的项(
FirstOrDefault
返回符合结果的第一个项,或者
null
如果没有找到任何项)。 如果找到正确的项目,则运行与OP中相同的代码


希望我没有犯任何错误,前面没有代码编辑器;)

“此为循环”-您是指这些为循环。你试过在调试器中单步执行并检查值吗?是的,问题是我试图比较两个值,正如你所看到的,但是循环不止一次地比较它们。每次
i
j
的值是什么?如果
分类2.parentCategoryId.Count
categories2.NewCategoryId.Count
是相同的,您只需要外部循环,并在两个循环上使用
i
变量。是否意味着其中的某个地方有一个
categories1
?或你只是不擅长命名东西吗?这是正确的,我的嵌套循环在那里,因为它们在列表中是相同的值,但它们被置乱了,所以它们不符合顺序。在这种情况下,遍历一个列表,在另一个列表上做“包含”测试。它仍然是一个有效的嵌套循环,但测试机制不同。你想测试的不是“序列相等”,而是“包相等”。@JensSvensson好的,我想我现在明白了,我会考虑一下,然后更新我的答案。这些是列表、数组还是可观察集合?和@madreflect