获取2个列表中未找到的值的最佳方法是什么? [英] What is the best way of getting values not found in 2 lists?

查看:74
本文介绍了获取2个列表中未找到的值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试比较2个列表的内容,并吐出列表中找不到的任何项目。我不知道最好的方法是什么,我试过这个:



 List< object1> list = GetList(); 
列表< object1> list2 = list;

foreach (列表g 列表中)
{
if (!list2.Contains(g))
{
Method1();
}
}





方法1是列表中每个项目的火灾,我需要一个更好的比较? object1是一个有多个字段的对象。



问候

Carl

解决方案

示例:

ListA:A,B,C

ListB:A,B,E



= >比较A和B:



List< string> result = ListA.Except(ListB).ToList();



=>结果:C



您可以在以下链接阅读更多信息:



http://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list [ ^ ]


不清楚你的具体要求是什么,但你可以尝试使用下面的linq

  if (!list.Any(g => list2.Contains(g))
{
Method1();
}


Hi guys,

I am trying to compare the contents of 2 lists and spit out any items in the list not found in both. I am not sure what the best method for this is, i have tried this:

List<object1> list = GetList();
List<object1> list2 = list;

            foreach(list g in list)
            {
                if (!list2.Contains(g))
                {
                    Method1();
                }
            }



Method1 is fires for each item in the list where it shouldnt, do i need a better comparer? object1 is an object with multiple fields.

Regards
Carl

解决方案

Example :
ListA : A,B,C
ListB : A,B,E

=> compare A with B :

List<string> result = ListA.Except(ListB).ToList();

=> result : C

You can read more at this link :

http://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list[^]


Unclear what is your exact requirement, but you can try with linq like below

if(!list.Any(g=>list2.Contains(g))
{
   Method1();
}


这篇关于获取2个列表中未找到的值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆