Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:19:42+00:00 2026-06-17T15:19:42+00:00

Trying to get a query that has both AND and OR to work in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:19:43+00:00Added an answer on June 17, 2026 at 3:19 pm

    You’re getting that back because you’re are putting all of your Query.EQ into the Query.Or on the last line. Passing in a single value to a Query.And doesn’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 Or together first, and pass it as the first parameter to the And, followed by the single, additional clause for Flag.

    var clauses = new[] { Query.EQ("Code", "abc"), Query.EQ("Description", "def") };
    var query = Query.AND(Query.OR(clauses), Query.EQ("Flag", 1))
    

    Something like that should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still trying to get the hang of SQL. I built 1 query that
I am trying to get my linq query to replicate my t-sql but I
I am trying to get a query that gives me the UserNames from table
I am trying to build a sql query that I think it involves inner
I have been trying to get this query to work for the longest and
I'm trying to get a paged query to work properly using LINQ and NHibernate.
How do I write a find(:all) query in rails (v2.3.5) that has parameters both
Hi I am trying to build an application which has models resembling something like
I'm trying to get the query string from a string (not the current URL).
I am trying to get my query to grab multiple rows while returning the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.