user1144596
user1144596

Reputation: 2098

Boolean LINQ Issue

I have a field in my database table called IsActive that can either be True or False. When I query the database, though, my query also returns the inactive rows:

This is my LINQ Query:

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

Upvotes: 2

Views: 180

Answers (1)

Corey Adler
Corey Adler

Reputation: 16149

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. 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).

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.

Upvotes: 12

Related Questions