views:

84

answers:

5

This is giving me a headache. I have this link query here that grabs an ID

Dim mclassID = From x In db.SchoolClasses Where x.VisitDateID = _visitdateID Select x.ClassID

And then later on I have this linq query

ViewData("Staff") = From t In db.Staffs Where t.ClassID = mclassID Select t

Any help would be much appreciated. I've tried quite a few things but to no avail. I've attempted casting, converting, Is operand, etc.

A: 

Just a guess, but would it help to wrap the right-side of the equation in parenthesis? I.e.:

ViewData("Staff") = (From t In db.Staffs Where t.ClassID = mclassID Select t)
dave
Nope, tried that one too.
keynone
+1  A: 

The problem is that myClassID is an anonymous IQueryable. You need to force it into another type (List is my favorite), and then pull it out of that type. So if you were to select it into a List(Of Integer) you could then extract the First() one since it would be the only. You could try something like this:

Dim myClassIDList As List(Of Integer) = New List(Of Integer)( _
    From x In db.SchoolClasses Where x.VisitDateID = _visitdateID Select x.ClassID)

Dim myClassID as Integer = myClassIDList.First()
Joel Etherton
Lol, kinda goofy but it worked! Thanks bro
keynone
@Kehnon Israel: If it worked, then mark it as the answer...
James Curran
It was answered so fast that I had to wait a few mins
keynone
You don't have to stuff the query into a list in order to call `First`. Also, it'd be better to call `ToList` if you want a list, rather than passing it into a `List` constructor. Also, you probably want `Single` or `SingleOrDefault` instead of `First`.
bdukes
@bdukes - I agree. There are several ways to accomplish the same result. I was even thinking there might be a more direct route to grab the integer. I did say, though, that the List is my favorite.
Joel Etherton
A: 

You should change this

Dim mclassID = From x In db.SchoolClasses Where x.VisitDateID = _visitdateID Select x.ClassID

to select a single instance or the first instance, otherwise it does as its reporting, returning an IEnumerable, which causes your error later.

Or you could change your second statement to something like

ViewData("Staff") = From t In db.Staffs Where mclassID.Contains(t.ClassID) Select t

taking advantage of the mclassID as an IEnumerable of int.

Jonathan Bates
A: 

have you tried:

ViewData("Staff") = From t In db.Staffs Where t.ClassID.equals(mclassID)  Select t
Patricia
yep, didn't quite work :/
keynone
what about .contains instead of .equals ?
Patricia
A: 

Using the Select operator, you can have multiple results returned. If you are only expecting one result (i.e. you are selecting by a primary key), then you can use Single or SingleOrDefault (depending on whether there is guaranteed to be a result) to get just that one.

Dim mclassID = (From x In db.SchoolClasses _
               Where x.VisitDateID = _visitdateID _
               Select x.ClassID).SingleOrDefault()
bdukes