检查存在性2 IEnumerable的之间存在、IEnumerable

2023-09-04 10:27:51 作者:小乖买糖吃

 的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());
 
一进入ie就是www.5913.net不是我本来的主页 我看了属性什么都没变 怎么会这样

查找的 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