检查存在性2 IEnumerable的之间 [英] check existense between two IEnumerable
本文介绍了检查存在性2 IEnumerable的之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的IEnumerable<字符串> existedThings =
从mdinfo在mdInfoTotal选择mdinfo.ItemNo;
IEnumerable的<字符串> thingsToSave =
从lbXReadSuccess.Items.Cast&LT项目,列表项>()选择item.Value;
下面是两个的IEnumerable
。
我要检查是否存在于existedThings一个值
thingsToSave
。
O.K。我能做到这一点与3号线code。
布尔hasItemNo;
的foreach(字符串货号在existedThings)
hasItemNo = thingsToSave.Contains(货号);
不过,看起来很脏。
我只是想知道是否有更好的解决方案。
解决方案
INT [] ID1 = {44,26,92,30,71,38};
INT [] ID2 = {39,59,83,47,26,4,30};
IEnumerable的< INT>无论= id1.Intersect(ID2);
的foreach(在两种INT id)的
Console.WriteLine(ID);
//Console.WriteLine((both.Count()大于0)的ToString());
Console.WriteLine(both.Any()的ToString());
IEnumerable<String> existedThings =
from mdinfo in mdInfoTotal select mdinfo.ItemNo;
IEnumerable<String> thingsToSave =
from item in lbXReadSuccess.Items.Cast<ListItem>() select item.Value;
Here are two IEnumerable
.
I want to check whether a value in existedThings
exist in thingsToSave
.
O.K. I can do that with 3 line code.
bool hasItemNo;
foreach(string itemNo in existedThings)
hasItemNo= thingsToSave.Contains(itemNo);
But, It looks dirty.
I just want to know if there is better solution.
解决方案
int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };
IEnumerable<int> both = id1.Intersect(id2);
foreach (int id in both)
Console.WriteLine(id);
//Console.WriteLine((both.Count() > 0).ToString());
Console.WriteLine(both.Any().ToString());
Look for the Enumerable.Intersect Method
这篇关于检查存在性2 IEnumerable的之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文