Cannot check the same property twice using dynamic linq

asp.net-mvc c# dynamic-linq linq

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?

1
2
4/17/2013 3:36:58 PM

Accepted Answer

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\""
2
4/17/2013 3:53:20 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow