Linq查询到数组

时间:2014-04-02 19:49:32

标签: vb.net linq linq-to-objects

这是我目前的Linq查询:

result = From d In dc.Tracker
   Where d.Responsible.Value = "first last name"
      Order By d.Priority1, d.Deadline
   Select New With { _
      Key .Title = d.Name, _
      Key .Description = d.Priority1, _
      Key .Priority2= d.Priority2, _
      Key .Status = d.Status, _
      Key .Resp= d.Resp, _
      Key .Deadline = d.Deadline, _
      Key .Notes = d.Notes, _
   }

我正在尝试输出数据,但似乎只能这样做:

For Each d In result
   Console.WriteLine(d)
Next

但我似乎无法将其放入数组中,以便我可以这样称呼它:

result(0), result(1), etc etc...

甚至更好:

result.Title, result.Description, etc etc...

如何将linq更改为数组?

2 个答案:

答案 0 :(得分:0)

使用.ToArray()方法。

result = (From d In dc.Tracker
          Where d.Responsible.Value = "first last name"
          Order By d.Priority1, d.Deadline
          Select New With { _
           .Title = d.Name, _
           .Description = d.Priority1, _
           .Priority2= d.Priority2, _
           .Status = d.Status, _
           .Resp= d.Resp, _
           .Deadline = d.Deadline, _
           .Notes = d.Notes}).ToArray()

答案 1 :(得分:0)

至少我的工作方式也是如此:

Dim Title As New ArrayList
Dim Description As New ArrayList
Dim Priority2 As New ArrayList
Dim Status As New ArrayList
Dim Resp As New ArrayList
Dim Deadline As New ArrayList
Dim Notes As New ArrayList

result = From d In dc.Tracker
   Where d.Responsible.Value = "first last name"
      Order By d.Priority1, d.Deadline
   Select New With { _
      Key .Title = d.Name, _
      Key .Description = d.Priority1, _
      Key .Priority2= d.Priority2, _
      Key .Status = d.Status, _
      Key .Resp= d.Resp, _
      Key .Deadline = d.Deadline, _
      Key .Notes = d.Notes, _
   }

For Each d In result
    If Not IsNothing(d.Title()) Then Title.Add(d.Title()) Else Title.Add("NA")
    If Not IsNothing(d.Description()) Then Description.Add(d.Description().value) Else Description.Add("NA")
    If Not IsNothing(d.Priority2()) Then Cost.Add(d.Priority2().value) Else Priority2.Add("NA")
    If Not IsNothing(d.Status()) Then Status.Add(d.Status().value) Else Status.Add("NA")
    If Not IsNothing(d.Resp()) Then Responsible.Add(d.Resp().value) Else Resp.Add("NA")
    If Not IsNothing(d.Deadline()) Then Deadline.Add(d.Deadline()) Else Deadline.Add("NA")
    If Not IsNothing(d.Notes()) Then Notes.Add(d.Notes()) Else Notes.Add("NA")
Next