list对比差异

List<string> listA = new List<string> { "1", "2", "3", "4" };
List<string> listB = new List<string> { "2", "3", "4", "5" };

// 使用 Except 找出 listA 中不在 listB 中的元素
List<string> difference = listA.Except(listB).ToList();

if (difference.Any())
{
     Console.WriteLine("存在 listA 中有元素不在 listB 中:");
     foreach (var item in difference)
     {
         Console.WriteLine(item);
     }
}
else
{
     Console.WriteLine("listA 中的所有元素都存在于 listB 中。");
}

posted @ 2024-11-22 17:03  网络来者  阅读(38)  评论(0)    收藏  举报