如何基于另一个列表的元素迭代列表

时间:2016-05-24 08:56:25

标签: c# asp.net

我有两个列表。想象一下:

List<string> firstList = {"ab","bb","cd","ed","fe","ff","eg","lh","di","dj"}

List<string> secondList = {"ab","fe","lh","dj"}

我需要遍历firstList和&#34;做一些事情&#34;对于secondList中存在的每个元素。可能?请注意,在"do something部分,我需要Add firstList的下一个元素到另一个List。请指导。

我的工作是:

for (int g = 0; g < row_2.Count; g++)
{
     for (int h = 0; h < d["topHeadings"].Count; h++)
     {
         if (row_2[g] == d["topHeadings"][h])
         {
             while ((row_2[g] != d["topHeadings"][h + 1]))
                    {
                           row_2_1.Add(row_2[g + 1]);
                            g++;
                    }
          }
     }
}

5 个答案:

答案 0 :(得分:3)

使用Except

List<char> firstList = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
List<char> secondList = new List<char>() { 'a', 'd', 'e', 'j' };
bool Result = secondList.Except(firstList).Any();

如果true中的元素不在secondList

,则firstList$rules = array( 'file' => 'mimes:jpg,png,pdf,doc,docx', 'file' => 'validate_file' );

答案 1 :(得分:2)

您可以使用AllContains

执行此操作
bool allInFirst = secondList.All(entry => firstList.Contains(entry));

如果true中的所有元素也包含在secondList中,则firstListfirstList

根据你的评论,你想要的东西与你的问题完全不同。您似乎想要遍历secondList和&#34;做某事&#34;对于foreach(var element in firstList.Where(secondList.Contains)) // do something with element 中存在的每个元素,您可以这样做:

firstList

这会对secondList中存在的元素进行过滤firstList并对其进行迭代。也许我再次误解了你,你想要反过来,所以只需交换secondListfor (int firstIndex = 0; firstIndex < firstList.Count; firstIndex++) { int secondIndex = secondList.IndexOf(firstList[firstIndex]); if (secondIndex < 0) continue; // do what you need with firstList[firstIndex] and the indices }

如果您需要指数,可能最好的方法是:

<association property="TermDto" column="ID"  resultMap="select-main-result"  />

答案 2 :(得分:1)

只是提供第三个选项,您可以使用HashSet<T>作为第二个集合,然后您已经拥有正确的方法:HashSet<T>.IsSubsetOf

HashSet<char> secondChars = new HashSet<char>() { 'a', 'd', 'e', 'j' };
bool allSecondInFirst = secondChars.IsSubsetOf(firstList);

您可以通过将列表传递给构造函数来动态初始化集:

HashSet<char> secondChars = new HashSet<char>(secondList);

但是,如果不允许重复,那么首先使用该套装会更有效率。

答案 3 :(得分:1)

我的代码比其他代码更广泛,但它是如何迭代列表并在两个列表之间重合时返回true的基本示例:

List<string> firstList = new List<string>() {"a", "b", "c", "d", "e", "f"};
        List<string> secondList = new List<string>() { "a", "c", "e" };
        bool elementFound = false;
        int countFirst = 0;
        int countSecond = 0;

        while ((countFirst < firstList.Count) && (!elementFound))
        {
            elementFound = false;

            while ((countSecond < secondList.Count) && (!elementFound))
            {
                if (secondList[countSecond] == firstList[countFirst])
                {
                    elementFound = true;
                }

                countSecond++;
            }

            if (!elementFound)
            {
                countFirst++;
            }
        }

        Console.WriteLine("We have found an element from the second list in the first one.");
        Console.WriteLine("The element is: {0}", firstList[countFirst]);
        Console.WriteLine("Pos: {0}", countFirst);

当然,我已经使用字符串List。

制作了示例

答案 4 :(得分:0)

出于性能原因,您可能希望使用HashSet。像这样:

var elementsFromFirstList = new HashSet<int>(firstList);
secondList.Where(elem => elementsFromFirstList.Contains(elem)); // these are all elements that are present in the first list and the second list

或者选择:

secondList.Intersect(firstList); // these are all elements that are present in the first list and the second list

编辑: 如果需要第一个列表中元素的索引,可以使用IndexOf方法。但是,这可能不会给你想要的性能。我会建议这样的事情:

var index = 0;
var indexByElement = firstList.ToDictionary(elem => elem, elem => index++);
foreach(var elem in secondlist.Where(indexByElement.ContainsKey))
{
    var index = indexByElement[elem];
    // do something
}