.net - Linq从整数列表中选择多个ID(主键)

标签 .net linq-to-sql lambda

我有一个整数列表

''# 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/

相关文章:

c# - LINQ 删除结果

c# - 查询不返回任何东西

java - 总结列表的每 N 个元素?

java - 如何将这个经典的 Java 代码重写为 Java Stream API 代码?

c# - 嵌套泛型函数的类型推断

asp.net-mvc - 如果我没有编写用于实例化该对象的类,我可以序列化该对象吗?

c# - 为什么在调用者而不是有问题的行上抛出异常

c# - Outlook 中的 XML 功能区 - 使其显示在特定窗口中

asp.net - GetCountries 从 .NET 不提供所有国家?

c# - 从序列末尾获取元素的linq扩展方法