Linq子列表问题

时间:2009-04-29 12:32:45

标签: linq

我对Linq很新,我无法将以下内容转换为linq表达式:

        Dim returnedInstructions As List(Of Integer) = New List(Of Integer)
        For Each j As Job In response.Jobs
            For Each i As Instruction In j.Instructions
                returnedInstructions.Add(i.InstructionId)
            Next
        Next 

任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:2)

我对VB不太熟悉,如果这与C#语法有些混淆,请道歉。

returnedInstructions  = 
(from j in response.Jobs _
from i in j.Instructions _
select i.InstructionId).ToList();

答案 1 :(得分:0)

这样的事情应该有效:

    Dim returnedInstructions As New List(Of Integer)
    Dim q = From j In response.Jobs, i In j.Instructions Select i.InstructionID
    returnedInstructions.AddRange(q)

答案 2 :(得分:0)

你想要SelectMany

List<int> returnedInstructions = response.Jobs
  .SelectMany(j => j.Instructions
    .Select(i => i.InstructionID))
  .ToList();

(现在在VB中,我不知道这是否编译)

Dim returnedInstructions = response.Jobs _
  .SelectMany( Function(j) j.Instructions _
    .Select( Function(i) i.InstructionID) ) _
  .ToList()