Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

I ran into this problem this morning and thought I’d share in case someone else has this issue .So I’m using PredicateBuilder with the EntityFramework and doing some pretty basic query building.

I have a list of business types that the person can select (a checkboxlist) and then it returns everything that matches. I was having an issue because it was only ever returning the businesses that matched the last item I selected—it wasn’t stacking the query at all.

Finally, I noticed what was going on. Inside my for loop:

foreach (int bTypeId in businessTypeIds)
            {
              
                businessTypes = SearchHelper.Or(businessTypes,
                    m => m.BusinessType.BusinessTypeID == bTypeId);
                businessFiltered = true;
            }

 

I was never casting bTypeId to its own variable. So if I ran a query to search for business type Ids 23 and 24, it was going through once ,and asking for == 23. Then it went through the second time, and changed the reference to both to be == 24. So the query ended up just saying does the business type equal 24 two times, instead of saying does it equal 23 or 24.

The quick fix? Add in a temporary variable and use that instead:

foreach (int bTypeId in businessTypeIds)
            {
                int tempId = bTypeId;
                businessTypes = SearchHelper.Or(businessTypes,
                    m => m.BusinessType.BusinessTypeID == tempId);
                businessFiltered = true;
            }

 

Now the tempId holds the proper Id for each type, and the query will essentially read “does it equal 23 or 24”.

Posted on Thursday, August 19, 2010 10:40 AM | Back to top


Comments on this post: quick fix: Predicatebuilder not stacking queries issue inside for loop

# re: quick fix: Predicatebuilder not stacking queries issue inside for loop
Requesting Gravatar...
Thx - I had the exact same problem and your fix worked like a charm.
Left by Eric Pincus on Oct 05, 2010 4:57 PM

# re: quick fix: Predicatebuilder not stacking queries issue inside for loop
Requesting Gravatar...
hey man, i was going to shoot myself in the head!
thank you so much for sharing,
i'll be glad to share with you my experiences too,
good luck...
Left by reza on Dec 02, 2010 6:28 AM

# re: quick fix: Predicatebuilder not stacking queries issue inside for loop
Requesting Gravatar...
You're a life saver! I don;t think I would have ever figured that one out. Thanks!
Left by Lloyd on Dec 30, 2011 12:15 PM

# re: quick fix: Predicatebuilder not stacking queries issue inside for loop
Requesting Gravatar...
Thank you a lot for this post, it helped me a lot, I was trying to resolve this problem for several hours with no luck until i came across your post, thanks again.
Left by bary on Aug 24, 2012 5:43 PM

# re: quick fix: Predicatebuilder not stacking queries issue inside for loop
Requesting Gravatar...
Thank you for the post.
Left by Joel on May 29, 2015 12:01 AM

Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net