I have a query using LINQ-to-SQL. It queries an underlying database table Rooms. It uses Where conditions to narrow down the results, namely:
- Gender.
- Current Occupancy < Max Occupancy
- Available Flag is Checked
I know this should return results but it keeps returning an empty set. Any ideas? Code is below
Dim selectedHalls = (From sh In dbHalls.Rooms _
Where sh.gender = Session("gender").ToString _
Where sh.max_occupancy > sh.current_occupancy _
Where sh.is_available = 1 _
Select sh.building_name).Distinct()
UPDATE: I’ve verified that the issue is with the statement where sh.is_available = 1, which doesn’t make sense since this is a bit field.
I think the best way to find out what the problem is to generate the SQL string and test your assumption that “it should return results” by executing it directly against the data source.
To do this:
(I usually use C#, but I think that’s how you declare a string in VB, right?)
Anyway, this will give you an SQL statement that will be more informative than anything we can give you without being able to look at your underlying database.
The only thing that jumps out at me as being potentially problematic is the
Session("gender"). You are basically relying on your Session object to be populated, have a value for the case-sensitive string key"gender"that matches the case-sensitive string fieldgenderin your database. This sounds like quite a few assumptions, that may or may not be tested and could be the reason for receiving empty results.EDIT
I just saw your update. Linq-to-sql, interprets a bit field as a boolean value, not an integer value. Try changing it to just
where sh.is_available