Question

I'm building an MVC app which will have to build dynamic linq queries to the server, and I'm facing something that puzzles me.

Up to now I'm able to make a query like this:

var objQry = from o in m_DB.OBJECTS.Where(whereConditions)
             select c;

if(!objQry.Any())
{
    return null;
}

And this works fine. The "whereConditions" variable is a string built before. Here's an example of a valid Query string:

OBJ_NAME == \"Sword\" and OBJ_OWNER == \"Stan\"

This will return any object whose name is "Sword" owned by "Stan". But, strangely, if I do make a query string using twice the same parameters, like this:

OBJ_COLOR == \"Blue\" and OBJ_COLOR == \"Red\"

Even thought I can testify that my database DOES have single "blue" items and single "red" items, this string will return null. Can anyone help me out?

Was it helpful?

Solution

If I understand you correctly, you want to return all items where the colour is red or blue. Your current query returns items where the item's OBJ_COLOR is both "Red" and "Blue". This won't return anything as it is a contradiction - it cannot be both.

I believe you want to use the OR operator instead:

"OBJ_COLOR == \"Blue\" OR OBJ_COLOR == \"Red\""
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top