我有一个整数列表
''# VB
Dim ResultIDsas List(Of Integer) = new List(Of Integer)
// C#
List<int> ResultIDs= new List<int>();
我通过循环 Lucene 读取的结果来添加到该列表。
''#VB
While (i <= (page * 10) AndAlso i < HitCollection.Length)
Dim document As Document = HitCollection.Doc(i)
Dim _event As New Domain.[Event]
ResultIDs.Add(document.[Get]("ID"))
i += 1
End While
// C#
while ((i <= (page * 10) && i < HitCollection.Length)) {
Document document = HitCollection.Doc(i);
Domain.Event _event = new Domain.Event();
ResultIDs.Add(document.Get("ID"));
i += 1;
}
现在问题来了。
假设我的整数列表是[1,5,6,19,22]
当我需要查询我的服务时,linq (lambda) 表达式会是什么样子?
''# VB
EventService.QueryEvents().Where(Function(e) (e.ID = 1))
// C#
EventService.QueryEvents().Where((System.Object e) => (e.ID == 1));
// Obviously these will simply grab ID of "1" which is not what we want.
最佳答案
EventService.QueryEvents().Where(e => list.Contains(e.ID));
这将生成相当于 SELECT ... WHERE e.ID in (1,5,...)
关于.net - Linq从整数列表中选择多个ID(主键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372484/