本文介绍了我有两个表发现未接元件数量和成立元素数量在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于两个列表A和B:
A = {6,16,22}
B = {6,7,8,22,24,25}
我想:
1。出现在两个列表中的项数。 (例如2,因为6和22从`A`,可以在`B`。)
2.这是在`A`但不是在`B`的项数。 (例如,1,因为16是`A`但不是在`B`)
项目3.数算相邻号码为1(例如,6是有乙6,7,8有这么6,7,8算作1)
解决方案
您可以使用的和的:
VAR aNotInB = a.Except(B);
INT aNotInBCount = aNotInB.Count(); // 1,因为16是不是在第二份名单
VAR aInB = a.Intersect(B);
INT aInBCount = aInB.Count(); // 2,因为6,22在第二份名单
Given two lists A and B:
A={6,16,22}
B={6,7,8,22,24,25}
I want to get:
1. the number of items that appear in both lists. (E.g. "2", because 6 and 22 from `A` are also in `B`.)
2. the number of items that are in `A` but not in `B`. (E.g. "1", because 16 is in `A` but not in `B`)
3.The number of items count adjacent numbers as 1 (E.g, A 6 is there B 6,7,8 is there so 6,7,8 count as 1 )
解决方案
You can use Enumerable.Except
and Enumerable.Intersect
:
var aNotInB = a.Except(b);
int aNotInBCount = aNotInB.Count(); // 1 because 16 is not in second list
var aInB = a.Intersect(b);
int aInBCount = aInB.Count(); // 2 because 6,22 are in second list
这篇关于我有两个表发现未接元件数量和成立元素数量在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!