4
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.Collections.Generic.List(Of String)'.

1
  • Make sure you have Option Strict On. If you have Option Strict Off, your Query may be infering the result as IEnumerable(Of Object) rather than string and ToList isn't casting it properly. Otherwise, remove the As List(Of String) and see what type inferencing returns. I suspect you have an implicit cast happening somewhere that the compiler is hiding. Commented Sep 2, 2011 at 17:36

4 Answers 4

13

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()
Sign up to request clarification or add additional context in comments.

8 Comments

not seems to work: Value of type 'System.Collections.Generic.List(Of System.Collections.Generic.IEnumerable(Of Char))' cannot be converted to 'System.Collections.Generic.List(Of String)'.
@serhio: mmm - if that's the case, then .ToList() should work fine.
Edited my answer - that's effectively the same as what you're doing in the "good old method"...
As I indicatd in the post, such a method does not exist. ToList, because "query" is IEnumerable(Of String)
@serhio: The LINQ extension method Enumerable.ToList works just fine on an IEnumerable(Of String). Your first version should work.
|
1

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.

Comments

0

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.

Comments

-1

Finally, solved with good old method : )

  Dim values As New List(Of String)

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

4 Comments

Your question was how to do something with Linq. You shouldn't incorrectly answer your own question and then accept it.
@Yatrix Don't worry! Up votes will reveal the result :-)
@FaizanMubasher Sure, but the fact still remains this is the wrong answer.
This is what I am saying. Up votes are revealing what is right answer :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.