LINQ-to-SQL 查询不返回结果
我有一个使用 LINQ-to-SQL 的查询。它查询底层数据库表 Rooms。它使用Where条件来缩小结果范围,即:
- 性别。
- 当前入住率 < 最大占用
- 已选中
可用标志我知道这应该返回结果,但它一直返回空集。有什么想法吗?代码如下
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()
更新:我已经验证问题出在语句 where sh.is_available = 1 上,这没有意义,因为这是一个位字段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为找出问题所在的最佳方法是生成 SQL 字符串,并通过直接针对数据源执行它来测试“它应该返回结果”的假设。
为此:(
我通常使用 C#,但我认为这就是在 VB 中声明字符串的方式,对吧?)
无论如何,这将为您提供一条 SQL 语句,该语句比我们无法查看的情况下提供的任何信息都更丰富。在您的底层数据库中。
唯一让我觉得可能有问题的是
Session("gender")
。您基本上依赖于要填充的 Session 对象,区分大小写的字符串键"gender"
的值与您的 Session 中区分大小写的字符串字段gender
相匹配。数据库。这听起来像是相当多的假设,可能会也可能不会被测试,并且可能是收到空结果的原因。编辑
我刚刚看到你的更新。 Linq-to-sql,将位字段解释为布尔值,而不是整数值。尝试将其更改为
where sh.is_available
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 fieldgender
in 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
我没有发现您的 LINQ 有任何明显的问题,因此请尝试删除Where 子句并一次将它们恢复一个。这应该让你知道是什么导致了这个问题,尽管我同意安德鲁·沃格尔的观点,即性别条款可能是可疑的。
如果在所有Where子句都消失后查询返回空,那么您就会知道这里还发生了其他事情。
I don't see anything obviously wrong with your LINQ, so try removing the Where clauses and bringing them back one at a time. That should let you know what's causing the issue, though I agree with Andrew Vogel that the gender clause is a likely suspect.
If the query comes back empty after all the Where clauses are gone, then you'll know there's something else going on here.
你真的在 dbHalls.Rooms.sh 中将他们的性别记录为“女”和“男”吗?
Do you really record their genders as "Female" and "Male" in dbHalls.Rooms.sh?