开发者

Linq query exception/error

开发者 https://www.devze.com 2023-03-17 10:11 出处:网络
Following is the code I get to achieve a list of Courses: Dim Result As New List(Of WorkMateLib.CourseNameSpace.CoursesLib.Course)

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 '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.

Dim Result = Courses.CoursesOfferedMAIN.Values                    _
                    .SelectMany(Function(cat) cat.Courses.Values) _
                    .ToList
0

精彩评论

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

关注公众号