开发者

Linq query to list VB.NET

Dim query = From o In myContainer.MyObjects Select o.MyStringProperty Distinct

Dim myProperties As List(Of String) = query.ToList????? 'no way!!!'

"query" type is IEnumerable(Of String)

I tried to use the query directly as a DataSource of a (infragistic) combobox, but it throws me NullReferenceException, so I decided to convert it to a listof strings, to be a more "classical" datasource.

Dim values As List(Of String) = query.AsQueryable().ToList()

does not work either: Value of type 'System.Collections.Generic.List(Of System.Linq.IQueryable(Of String))' cannot be converted to 'System.Collection开发者_如何学Cs.Generic.List(Of String)'.


Your error message suggests the query is actually a List(Of T) containing a collection internally.

If this is the case, you can use SelectMany to flatten the collection:

Dim values As List(Of String) = query.SelectMany(Function(m) m).ToList()

Edit: Given your edit, as well as the comment, the following should work fine:

Dim values As List(Of String) = query.ToList()


Perhaps you should try to specify the type on the query variable?

Dim query = From o In myContainer.MyObjects Select o.MyStringProperty Distinct

Should become

Dim query As IEnumerable(Of String) = From o In myContainer.MyObjects Select o.MyStringProperty Distinct

If the type of IEnumerable(Of String) can't be used on query, then you need to flatten the list as Reed Copsey said.


This works for me:

Dim drs As List(Of ProjectTask) = (From pt In projectTasks Where pt.TaskEngineerId <> 1).ToList()

Putting brackets around the query did the trick.


Finally, solved with good old method : )

  Dim values As New List(Of String)

  For Each item In query
    values.Add(item)
  Next item
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜