简体   繁体   English

布尔LINQ问题

[英]Boolean LINQ Issue

I have a field in my database table called IsActive that can either be True or False . 我的数据库表中有一个名为IsActive的字段,可以是TrueFalse When I query the database, though, my query also returns the inactive rows: 但是,当我查询数据库时,我的查询也会返回非活动行:

This is my LINQ Query: 这是我的LINQ查询:

var j = from x in db.DashboardMenus 
        where x.ParentID == c.SiteMenuID && c.IsActive == true 
        orderby x.SortOrder ascending 
        select x;

You've got a typo in your query. 您的查询中有拼写错误。 You're using c.IsActive when you should be using x.IsActive , since that's the table object you're trying to query. 当你应该使用c.IsActive时,你正在使用x.IsActive ,因为那是你想要查询的表对象。 Otherwise it will return all x objects where the ParentID and SiteMenuID match as long as c is active (which appears to be your problem). 否则,只要c处于活动状态(这似乎是您的问题),它将返回ParentID和SiteMenuID匹配的所有x对象。

var j = from x in db.DashboardMenus 
        where x.ParentID == c.SiteMenuID && x.IsActive
        orderby x.SortOrder ascending 
        select x;

In the future, you might be better served by having more descriptive (and longer) variable names (even for LINQ queries) to make better sure that you don't run into this problem again. 在将来,通过使用更具描述性(和更长)的变量名称(甚至是LINQ查询)可以更好地为您提供更好的服务,以便更好地确保您不会再次遇到此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM