c# - 检查两个 IEnumerable 之间是否存在

标签 c# .net linq

IEnumerable<String> existedThings = 
        from mdinfo in mdInfoTotal select mdinfo.ItemNo;

IEnumerable<String> thingsToSave = 
        from item in lbXReadSuccess.Items.Cast<ListItem>() select item.Value;

这里有两个IEnumerable

我想检查 thingsToSave 中是否存在 value in existedThings

好的。我可以用 3 行代码做到这一点。

bool hasItemNo;
foreach(string itemNo in existedThings)
    hasItemNo= thingsToSave.Contains(itemNo);

但是,它看起来很脏。

我只是想知道有没有更好的解决方案。

最佳答案

 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());

寻找 Enumerable.Intersect Method

关于c# - 检查两个 IEnumerable 之间是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3013833/

相关文章:

c# - 如何在 Android 中使用信号器

c# - 卸载 CodeDom 编译的程序集

c# - 如何使用Unity Toggle Check Function(选中和未选中)

C# 生成新任务

c# - 通过托管代码 (.NET)/命令行启用 Windows 事件日志

c# - 如何最好地优化这一小段 C# Linq 代码

c# - Exception.ToString() 抛出异常

c# - 使用 .NET 的单个命令将 SQL INSERT 和 SELECT 插入 Oracle

c# - 从 GridView C# 中的 DataRow 获取原始对象

c# - 坚持 linq 查询