2

I'm having a problem with a LINQ to SQL query, the data being returned from the server could be null so I've set up an if statement within the query but the query is still throwing an exception.

This is a shortened down version of the query code

var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
                true :
                //The following line causes the exception to be thrown
                object.Equals(Query.Location.ToLower() , b.Location.ToLower())
        ) : 
        (
            (Query.Location == null) ?
                true :
                false
       )
       select b;

If the search term "Location" is null then I don't want to filter by location, but if it isn't null then I have to check if the value in the row is null or not as some of entries have a null location.

The code works fine until I add in the compare line. In order to get to the compare line, both Query.Location and b.Location cannot be null therefore the code shouldn't fail.

Any idea what the problem could be?

Thanks.

EDIT

If i remove the .toLower() from the object.equals call then the query runs correctly, it also manages to work no matter what case the query is in.

var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
                true :
                //The following line causes the exception to be thrown
                object.Equals(Query.Location , b.Location)
        ) : 
        (
            (Query.Location == null) ?
                true :
                false
       )
       select b;
3
  • if b.Location is null and Query.Location is not, do you need to select no rows? Commented Jun 30, 2011 at 10:38
  • if b.Location is null and Query.Location is null, do you need to select all rows? Commented Jun 30, 2011 at 10:41
  • if b.location is null and query.location is not then don't return it if b.location is null and query.location is null then return Commented Jun 30, 2011 at 10:45

3 Answers 3

3

I wouldn't like to say for sure what's going wrong here, but I think you could actually make your code significantly simpler by splitting it into two different queries:

public void Search(SearchTerms Query)
{
    var queryWithLocation = db.branches.Where(b =>
          Query.Location.Equals(b.Location, StringComparison.OrdinalIgnoreCase);

    var query = Query.Location != null ? queryWithLocation : db.branches;
}

I've changed the way of doing the Equals - that's the way I prefer to perform case-insensitive searches; you'll have to see whether it works for LINQ to SQL.

Sign up to request clarification or add additional context in comments.

3 Comments

@abatishchev: Where do you think I'm performing a query if Query.Location is null? Calling Where doesn't actually perform a query...
I could split it up but location is 1 of 3 search terms that could be null. so I would need quite a few queries, 1 for each possible combination. e.g. queryWithLocation, queryWithX, queryWithLocationAndX
@Midimatt: No, use composition. Build the second query off the first, etc.
0

Try this;

public void Search(SearchTerms Query)
{
var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
            true
            :
            //The following line causes the exception to be thrown
            Query.Location.ToLower() == b.Location.ToLower()

        )
        : 
        (
            (Query.Location == null) ?
            true
            :
            false
       )
       select b
}

I believe the object is probably causing the NullReference

2 Comments

Have you attached a debugger to determine 'what' is null?
I'm running it through vs2010 and stepping through the code, but I can't see what is null.
0
from b in db.branches
let location = b.Location
where location != null ?
    b.Location.Equals(b.Location, Query.Location ?? b.Location, StringComparison.OrdinalIgnoreCase) : // if Query.Location is null then select all
    false // to select nothing in this case
select b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.