在列表中查找交集并删除 [英] Find intersection and remove in a list

查看:103
本文介绍了在列表中查找交集并删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有以下代码,工作正常,但我正在寻找更好的解决方案。

=== =编辑====

列表中的项目是:

NewContainsExistingMemberInDialog:B,C,D,E

DialogAdditionDistinct:A,B,C



最终输出:

IntersectedAndRemovedList是DialogAdditionDistinct。它应该只包含A。



============

列表与LT; INT> ItemsToBeDeletedAtIndex =  new  List< int>(); 
int index = -1;

foreach string itemCAD in DialogAdditionDistinct)
{
index ++;
foreach 字符串 itemNCEMID in NewContainsExistingMemberInDialog)
{
if (itemNCEMID == itemCAD)
{
ItemsToBeDeletedAtIndex.Add(index);
}
}
}

int indexCounter = 0 ;
foreach int itemIndex in ItemsToBeDeletedAtIndex)
{
DialogAdditionDistinct.RemoveAt(itemIndex - indexCounter);
indexCounter ++;
}





此代码的作用是在两个列表之间找到相同的项目并保存项目的索引。之后删除了匹配索引中的项目。



所以基本上是有一个LINQ代码或者这个查找的优化代码版本两个列表之间的交集和删除相交其中一个项目。



非常感谢您的时间和理解。

解决方案

  foreach  string  itemNCEMID  in  NewContainsExistingMemberInDialog)
{
if (DialogAdditionDistinct.Any(accEntity = > accEntity.Equals(itemNCEMID)))
{
DialogAdditionDistinct.Remove(n);

}
}


LINQ中有Except方法,在你的例子中应该用以下方式。







 List< string> a =  new 列表< string>(){  A   B  C  D}; 
List< string> b = new 列表< string>(){ A B E F};

List< string> c = a。除了(b).ToList< string>();





你会在列表c中找到的是两个元素,C和D,一次包含在列表a中而不在列表中b。



通过在谷歌搜索,你会发现更多的例子。



干杯



编辑:

根据新信息,这是一个可能的解决方案:

 List< string> a =  new 列表< string>(){  B   C  D  E}; 
List< string> b = new 列表< string>(){ A B C};

a = b.Except(a).ToList< string>();





干杯


我认为这段代码适用于你的情况。



DialogAdditionDistinct = DialogAdditionDistinct.Except(NewContainsExistingMemberInDialog).ToList();

Hello all,

I had the following code which works fine but I am looking for a finer solution.
====EDIT====
My Items in the lists are ;
NewContainsExistingMemberInDialog: "B", "C", "D", "E"
DialogAdditionDistinct: "A", "B", "C"

Final output:
IntersectedAndRemovedList which is DialogAdditionDistinct. It should only contain "A".

============

List<int> ItemsToBeDeletedAtIndex = new List<int>();
                        int index = -1;

                        foreach (string itemCAD in DialogAdditionDistinct)
                        {
                            index++;
                            foreach (string itemNCEMID in NewContainsExistingMemberInDialog)
                            {
                                if (itemNCEMID == itemCAD)
                                {
                                    ItemsToBeDeletedAtIndex.Add(index);
                                }
                            }
                        }

                        int indexCounter = 0;
                        foreach (int itemIndex in ItemsToBeDeletedAtIndex)
                        {
                            DialogAdditionDistinct.RemoveAt(itemIndex - indexCounter);
                            indexCounter++;
                        }



What this code does finds the same items between the two list and saves the index of the item. After that removing the items in the matching index.

So basically is there a LINQ code or much optimized code version of this finding the intersection between two list and removing the intersected items from one of the list.

Thank you very much for your time and understanding.

解决方案

foreach (string itemNCEMID in NewContainsExistingMemberInDialog)
                        {
                             if (DialogAdditionDistinct.Any(accEntity => accEntity.Equals(itemNCEMID )))
                            {
                                DialogAdditionDistinct.Remove(n);

                            }
                       }


There is the Except method in LINQ and in your example should be used in the following way.



List<string> a = new List<string>() { "A", "B", "C", "D" };
List<string> b = new List<string>() { "A", "B", "E", "F" };

List<string> c = a.Except(b).ToList<string>();



What you will find in the list c are two elements, C and D, the once that are contained in the list a and not in the list b.

By searching on google you will find more examples.

Cheers

EDIT:
Based on new information this is a possible solution:

List<string> a = new List<string>() { "B", "C", "D", "E" };
List<string> b = new List<string>() { "A", "B", "C" };

a = b.Except(a).ToList<string>();



Cheers


I think this code will work in your case.

DialogAdditionDistinct = DialogAdditionDistinct.Except( NewContainsExistingMemberInDialog).ToList();


这篇关于在列表中查找交集并删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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