大家好,你们能帮我解决这个问题吗,我已经尝试了几件事。 我需要在两个 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/