的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());
查找的 Enumerable.Intersect方法
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