I am trying to get back a single from from a EF database model using the below line of code..
Dim _classRoom As classrm = db.classrms.Select(Function(b) b.Course_ID = _CurrCourse.course_ref)
Where classrm is the name of the entity and db is declared as a new entity. What I am trying to do is select a row from the entity based on matching Course_ID which in this model is a string. So that I can later use the variable _classRoom to get other items out of the same row.. However I am getting the following error:
Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[System.Boolean]' to type 'Trial_Online.classrm'.
Anyone got any ideas??? I have to preform similar tasks with several different Entities but if I get pointed in the right direction I can manage from there…
Use
Whereinstead ofSelectto filter the results, and addFirst()orSingle()to the end:The difference between
First()andSingle()is thatSingle()will throw an exception if there is more than 1 element in the result. Both will throw an exception if the result sequence is empty.You can also use
FirstOrDefault()andSingleOrDefault()to returnNothingif there are no results.