views:

452

answers:

1

I want to do something like this:

Dim selectedCourses As List(Of Guid) = From item In chkListCourses.Items Where item.Selected = True Select item.Value

but I get the error:

Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Object,System.Object]' to type 'System.Collections.Generic.List1[System.Guid]'.

The value of item is a string representation of a Guid.

I'd also like the syntax for a Lambda expression.

+1  A: 

You have to change your code to this:

Dim selectedCourses As List(Of Guid) = (From item In chkListCourses.Items Where item.Selected = True Select New Guid(item.Value)).ToList(Of Guid)
Ramezanpour
I get an error at New Guid(item.Value) Error 1 Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: 'Public Sub New(g As String)': Argument matching parameter 'g' narrows from 'Object' to 'String'. 'Public Sub New(b() As Byte)': Argument matching parameter 'b' narrows from 'Object' to '1-dimensional array of Byte'.
Micah Burnett
Oh sorry :D I thought it's C#. Change it to this:Dim selectedCourses As List(Of Guid) = (From item in chkListCourses.Items Where item.Selected = True Select Id = New Guid(item.Value)).ToList(Of Guid)
Ramezanpour
I get the same error at New Guid(item.Value)
Micah Burnett
Just cast item.Value to a string: CStr(item.Value)
Meta-Knight