【问题标题】:Comparing Element of list A and B and finding elements that are not present in List B?比较列表 A 和 B 的元素并查找列表 B 中不存在的元素?
【发布时间】:2014-03-09 07:48:38
【问题描述】:

我有两个列表

List<int> A= new List<int>()
{33,50,30,90,1,4,5,6,66,
}; 
and 
List<int> B=new List<int>()
   {50,4,33};

现在我想找出列表 A 中所有不在列表 B 中的元素

【问题讨论】:

  • 您好 user3126901,您能告诉我们您的尝试吗?某些示例代码不起作用,或者某些搜索字词无法为您提供正确的结果?

标签: c# .net list loops


【解决方案1】:
List<int> res = A.Except(B).ToList();

【讨论】:

    【解决方案2】:
    using System.IO;
    
    using System;
    
    using System.Linq;
    
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            List<int> A= new List<int>(){33,50,30,90,1,4,5,6,66,}; 
            List<int> B=new List<int>(){50,4,33};
    
            Console.WriteLine("using linq:");
    
            foreach(int i in A.Except(B).ToList())
            Console.WriteLine(i);
    
            Console.WriteLine("without using linq:");
    
            foreach(int i in Except(A,B))
            Console.WriteLine(i);
        }
    
        private static List<int> Except(List<int> A,List<int>B)
        {
            List<int> c = new List<int>();
            foreach(int i in A)
            {
                if(B.Contains(i))
                   continue;
                else
                   c.Add(i);
            }
            return c;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-18
      • 2019-09-06
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2021-06-27
      相关资源
      最近更新 更多