Trying to get a query that has both AND and OR to work in C#.
In SQL Server would be something like:
... where (Code = 'abc' OR Description = 'def') AND (Flag = 0)
With MongoVUE I seemed to have gotten it to work with this:
{ "$or" : [{ "Description" : /def/i }, { "Code" : /abc/i }], "$and": [{ "Flag" : 0 }] }
but can’t seem to get it with the C#:
tried this:
List<IMongoQuery> qryValue = new List<IMongoQuery>();
qryValue.Add(Query.EQ("Code", "abc"));
qryValue.Add(Query.EQ("Description", "def"));
qryValue.Add(Query.And(Query.EQ("Flag", 1)));
var query = Query.Or(qryValue.ToArray());
but get this back:
{ "$or" : [{ "Code" : "abc" }, { "Description" : "def" }, { "Flag" : 1 }] }
and this doesn’t give the correct results: missing the AND part.
Anyone can help with this?
You’re getting that back because you’re are putting all of your
Query.EQinto theQuery.Oron the last line. Passing in a single value to aQuery.Anddoesn’t do anything; it’s the same as if you passed a single value to any other method. You haven’t and-ed it with anything.You need to write it in code as you have above in SQL: respect the parenthesis.
Group your
Ortogether first, and pass it as the first parameter to theAnd, followed by the single, additional clause forFlag.Something like that should work.