我会Inner Join
在这种情况下进行。如果我会使用Contains
,它会Iterate unnecessarily
6 次despite of the fact that there is just one match
。
C#版本
var desiredNames = new[] { "Pankaj", "Garg" };
var people = new[]
{
new { FirstName="Pankaj", Surname="Garg" },
new { FirstName="Marc", Surname="Gravell" },
new { FirstName="Jeff", Surname="Atwood" }
};
var records = (from p in people
join filtered in desiredNames on p.FirstName equals filtered
select p.FirstName
).ToList();
VB.Net版
Dim desiredNames = New () {"Pankaj", "Garg"}
Dim people = New () {New With { _
.FirstName = "Pankaj", _
.Surname = "Garg" _
}, New With { _
.FirstName = "Marc", _
.Surname = "Gravell" _
}, New With { _
.FirstName = "Jeff", _
.Surname = "Atwood" _
}}
Dim records = ( _
Join filtered In desiredNames On p.FirstName = filtered).ToList()
包含的缺点
假设我有两个列表对象。
List 1 List 2
1 12
2 7
3 8
4 98
5 9
6 10
7 6
使用 Contains,它将搜索 List-2 中的每个 List-1 项目,这意味着迭代将发生 49 次!!!