0

我需要使用 LINQ 执行类似这样的 t-sql 语句:

SELECT * FROM mytable WHERE idnumber IN('1', '2', '3')

所以在 LINQ 我有:

Dim _Values as String = "1, 2, 3"
Dim _Query = (From m In mytable Where idnumber = _Values Select m).ToList()

不确定如何处理 _Values 以使 idnumber 评估字符串中的每个值。
提前致谢。

4

2 回答 2

3

我会Inner Join在这种情况下进行。如果我会使用Contains,它会Iterate unnecessarily6 次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 次!!!

于 2012-04-13T15:49:19.307 回答
1
Dim _Values as String = "1, 2, 3"
Dim _Query = (From m In mytable Where _Values.Split(", ").Contains(m.idnumber) Select m).ToList()
于 2012-04-13T15:45:23.243 回答