c# - 在两个 IEnumerable 之间搜索 - LINQ

标签 c# linq ienumerable

大家好,你们能帮我解决这个问题吗,我已经尝试了几件事。 我需要在两个 IEnumerable 之间进行搜索,这是代码。

IEnumerable<Project> Projects = new[] { new Project {id = "1", lan = "test1"},  new Project {id = "2", lan = "test1"}}

IEnumerable<string> lan = new [] { "test1", "test2"};
IEnumerable<string> indexFiltered = ?;

我需要做一个返回 Project.id 的 linq 查询,它在 lan 中有任何 Project.lan。

有什么想法吗?

最佳答案

我会使用 HashSet 而不是数组,因为它允许将 check-if-contains 作为 O(1) 而不是 O(n) 操作:

HashSet<string> lan = new HashSet<string> { "test1", "test2" };
IEnumerable<string> indexFiltered = projects
    .Where(p => lan.Contains(p.lan))
    .Select(p => p.id);

关于c# - 在两个 IEnumerable 之间搜索 - LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13269183/

相关文章:

c# - 如何在希伯来语中使用 String.Format

c# - 如何在sqlbulkcopy中设置自动映射

linq - 使用(Linq for JavaScript 库)子查询(操作方法)

c# - 是否可以使用 LINQ 从两个创建一维数组?

c# - 检查 IEnumerable 是否有任何行而不枚举整个列表

c# - "Unreachable code detected"switch/case 语句错误

c# - 将推特流复制到对象 : can I fall behind?

C#:如何测试 IEnumerable.GetEnumerator() 方法?

c# - 将 Outlook 电子邮件存储在 Microsoft SQL Server 数据库中

C# 扩展方法类型