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

发布于 2024-10-06 16:23:00 字数 1021 浏览 0 评论 0原文

我有一个整数列表,

''# VB
Dim ResultIDsas List(Of Integer) = new List(Of Integer)

// C#
List<int> ResultIDs= new List<int>(); 

通过循环 Lucene Read 的结果将其添加到该列表中。

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

I have an integer list

''# VB
Dim ResultIDsas List(Of Integer) = new List(Of Integer)

// C#
List<int> ResultIDs= new List<int>(); 

I add to that list by looping through the results of a Lucene Read.

''#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;
}

Now here's where the question comes in.

Say my List of Integers is [1,5,6,19,22]

What would a linq (lambda) expresion look like when I need to query my service?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-10-13 16:23:00
EventService.QueryEvents().Where(e => list.Contains(e.ID));

这将生成相当于 SELECT ... WHERE e.ID in (1,5,...)

EventService.QueryEvents().Where(e => list.Contains(e.ID));

this would generate the equivalent of SELECT ... WHERE e.ID in (1,5,...)

浅浅淡淡 2024-10-13 16:23:00

我在这里猜测,但这似乎就是你所追求的。

''# VB.NET
EventService.QueryEvents().Where(Function(e) (ResultIDs.Contains(e.ID))

在 C# 中:

// C#
EventService.QueryEvents().Where((System.Object e) => (ResultIDs.Contains(e.ID)));

I'm taking a guess here, but this seems to be what you're after.

''# VB.NET
EventService.QueryEvents().Where(Function(e) (ResultIDs.Contains(e.ID))

And in C#:

// C#
EventService.QueryEvents().Where((System.Object e) => (ResultIDs.Contains(e.ID)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文