LINQ的从整数列表

问题描述:

选择多个ID的(主键),我有一个整数列表LINQ的从整数列表

''# 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(拉姆达)expresion样子,当我需要查询我的服务?

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

我在这里猜测,但这似乎是你在之后。

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

而在C#:

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

真棒感谢。我尝试了另一种方式,因为我的大脑并不能正常工作lambda,但是`e.ID.Contains(ResultIDs)` - 这显然不起作用。 – 2010-12-07 01:19:52

+0

超酷。这就是Linq-to-SQL的工作方式,这是从SQL中的“Field IN(1,2,3)”转换为Linq中的“myList.Contains(Field)”的过渡。还有一件事--Contains()是IEnumerable()的一个扩展,所以如果这样做不方便,我认为你不需要依赖你的`ResultIDs`集合来成为IList。 – mattmc3 2010-12-07 01:41:03

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

这会产生SELECT相当于... WHERE e.ID在(1,5,...)