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

This may be a simple fix and maybe I'm missing something obvious, but when
I may be missing something obvious here, but how could I rewrite this code
This may be something covered in C# 101 but I haven't been able to
I realize something like this has been asked, but this may be a little
I may be missing something obvious here, but how could I rewrite this code!
I gave up on this one. May be someone can see something obvious. I
This may be something common and trivial, but I seem to be having trouble
I may be missing something obvious here, but in the devel module settings I
I may have missed something obvious with WPF, but is it possible to create
This may be obvious to you, but I'm definitely clueless. I basically have a

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.