Linq query exception/error
Following is the code I get to achieve a list of Courses:
Dim Result As New List(Of WorkMateLib.CourseNameSpace.CoursesLib.Course)
For Each cat As WorkMateLib.CourseNameSpace.CoursesLib.Category In Courses.CoursesOfferedMAIN.Values
For Each c As WorkMateLib.CourseNameSpace.CoursesLib.Course In cat.Courses.Values
Result.Add(c)
Next
Next
and it is working fine. However I am trying to a开发者_JAVA技巧ttempt to do the same with linq and the code is:
Result.AddRange(From k As WorkMateLib.CourseNameSpace.CoursesLib.Category In Courses.CoursesOfferedMAIN.Values Where Not k.CategoryName = "" Select k.Courses.Values.ToList())
But is throwing an error as follows:
Unable to cast object of type 'WhereSelectEnumerableIterator
2[WorkMateLib.CourseNameSpace.CoursesLib+Category,System.Collections.Generic.List
1[WorkMateLib.CourseNameSpace.CoursesLib+Course]]' to type 'System.Collections.Generic.IEnumerable`1[WorkMateLib.CourseNameSpace.CoursesLib+Course]'.
Could you please help me better understand it. Thank you.
This should be equivalent. Note that there's no need to create a list yourself.
Dim Result = Courses.CoursesOfferedMAIN.Values _
.SelectMany(Function(cat) cat.Courses.Values) _
.ToList
精彩评论