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 attempt 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
‘WhereSelectEnumerableIterator2[WorkMateLib.CourseNameSpace.CoursesLib+Category,System.Collections.Generic.List1[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.