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 329251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:34:15+00:00 2026-05-12T09:34:15+00:00

This may be something obvious but I’ve been banging my head against it for

  • 0

This may be something obvious but I’ve been banging my head against it for a few hours and can’t figure out where I’m going wrong.

I’m trying to run a small piece of code to test adding OR criteria to an NHibernate query. This is the code I have:

using (ISession session = NHibernateHelper.OpenSession())
{
    ICriteria criteria = session.CreateCriteria<TestObject>();

    int[] ids = {1, 2, 3};
    foreach (int id in ids)
    {
        ICriterion criterion = Restrictions.Eq("Id", id);
        criteria.Add(Restrictions.Disjunction().Add(criterion));
    }

    IList<TestObject> items = criteria.List<TestObject>();
    return items;
}

It’s just something simple that I would expect to return all test objects with IDs 1-3. But, when I’m running the code, the query generated is to find an object with ID = 1 AND ID = 2 AND ID = 3. Which, unsurprisingly, doesn’t return anything.

The mapping’s set up correctly (I can add/edit/remove/list all of the objects) and there are objects in there with these IDs.

Am I doing something obviously wrong? Any samples of using Disjunction I’ve seen online all seem to use it this way. I just don’t get why it keeps on using AND.

Thanks.

  • 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-05-12T09:34:16+00:00Added an answer on May 12, 2026 at 9:34 am

    Your problem lies in the fact that you’re creating a new disjunction every time (in a loop). What you need to do is:

    int[] ids = {1, 2, 3};
    ICriterion disjunction = Restrictions.Disjunction();
    foreach (int id in ids)
    {
        ICriterion criterion = Restrictions.Eq("Id", id)
        disjunction.Add(criterion);
    }
    criteria.Add(disjunction);
    

    The syntax may be a bit wrong – I’m a Hibernate guy rather than .NET 🙂

    To clarify, your original code would generate something like (in pseudo-code):

    WHERE (OR(ID=1)) AND (OR(ID=2)) AND (OR(ID=3))
    

    Since there’s nothing to “OR” to, disjunctions were silently omitted.

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

Sidebar

Related Questions

Ok, this may be a dumb question but here goes. I noticed something the
I may be missing something here because I thought this might be really easy
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This may seem like a daft question, but i was wondering about how to
This may be simple one, but 5 mins of Googling didn't give me the
This may be a simple fix - but I'm trying to sum together all
This may be a matter of style, but there's a bit of a divide
This may sound strange but sometimes when your ASP.NET webapp isn't working and you
This may be a doozy, but does anyone have an idea how to: Pass
This may be silly, but trying to do a dual list box in ASP.Net

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.