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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:16:47+00:00 2026-05-24T10:16:47+00:00

So I have a many to many relationship between something known as Specialism and

  • 0

So I have a many to many relationship between something known as Specialism and SpecialismCombo. What I’m trying to do is take an int[] of ids and check if there is a combo already that contains the specialisms with those ids.

I was close but not quite right.
Say I have specialisms with Ids 1 and 3 and I create a combo with those specialisms.

If I pass in 3 & 1 then it returns the expected combo id.

If I pass in 1 then it returns the combo id that has both 1 and 3.

I can’t just rely on total number of specialisms associated with the combo. Because if a combo has two items, 1 and 4 and the items being matched on are 1 and 3 I don’t want this coming back as a matched combo.

So it’s like I do need the count of this result, and match the count of total specialisms associated to the combo. I don’t quite get whether I’m after a subquery or detatchedcriteria or how to get the result I want using nhibernate criteria. Thanks for your help!

int[] SpecialismIds = ArrayExtensions.ConvertArray<int>(idCollection.Split(new char[] { '|' }));

    ICriteria query = m_SpecialismComboRepository.QueryAlias("sc");
        query.CreateAlias("sc.Specialisms", "s", NHibernate.SqlCommand.JoinType.InnerJoin);

    ICriterion lastCriteria = null;

    foreach(int i in SpecialismIds)
    {

         ICriterion currentCriteria = Restrictions.Eq("s.SpecialismId", i);
        if (lastCriteria != null)
                        lastCriteria = Restrictions.Or(lastCriteria, currentCriteria);
                    else
                        lastCriteria = currentCriteria;
    }

    if (lastCriteria != null)
                    query.Add(lastCriteria);

    IProjection IdCount = Projections.Count("s.SpecialismId").As("IdCount");

    query.SetProjection(
        Projections.GroupProperty("sc.SpecialismComboId"),
        IdCount 
        );

    query.Add(Restrictions.Eq(IdCount, SpecialismIds.Count()));

    var comboId = query.List();

The sql being generated is:

SELECT this_.SpecialismComboId as y0_, count(s1_.SpecialismId) as y1_ 
FROM dbo.SpecialismCombo this_ 
inner join SpecialismComboSpecialism specialism3_ on this_.SpecialismComboId=specialism3_.SpecialismId 
inner join dbo.Specialism s1_ on specialism3_.SpecialismComboId=s1_.SpecialismId WHERE s1_.SpecialismId = @p0 
GROUP BY this_.SpecialismComboId HAVING count(s1_.SpecialismId) = @p1',N'@p0 int,@p1 int',@p0=3,@p1=1

EDIT – It seems like I either need the having to be something like…

HAVING count(s1_.SpecialismId) = (select count(SpecialismId)
from specialismComboSpecialism
where SpecialismComboId = y0
group by SpecialismComboId) == @p2

Or maybe it’s simpler than that and I need to exclude SpecalismCombos where the combo.specialisms are not in the collection of ids.

Ie. if the combo has specialisms 1 and 3 but the collection only has 1.. then we could exclude this combo based on 3 not being in the collection…

Edit 8/8/2011
Went back to focusing on how to get the result I needed in SQL – and I believe this query works.

WITH CustomQuery AS
        (
        SELECT sc.SpecialismComboId,
        count(s.SpecialismId) AS ItemCount
        FROM SpecialismCombo sc 
        inner join SpecialismComboSpecialism scs on sc.SpecialismComboId = scs.SpecialismComboId
        inner join Specialism s on s.SpecialismId = scs.SpecialismId
        GROUP BY sc.SpecialismComboId 
        HAVING count(s.SpecialismId) = 2
        )

        SELECT CustomQuery.SpecialismComboId FROM CustomQuery
        INNER JOIN SpecialismComboSpecialism scs on CustomQuery.SpecialismComboId = scs.SpecialismComboId
        WHERE scs.SpecialismId in (1,4)
        GROUP BY CustomQuery.SpecialismComboId 
        HAVING count(scs.SpecialismId) = 2

So now I just need to figure out how to call this procedure from my nhibernate code passing in the appropriate values 🙂

I also discovered in the process that my mapping class was wrong – as it was putting the wrong values in the mapping table (ie. the specialismid was ending up in the specialismcomboid field !)

  • 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-24T10:16:47+00:00Added an answer on May 24, 2026 at 10:16 am

    So I ended up creating a stored proc and using SQL CTE in order to get only the specialism combos with the correct count of specialisms. Posting this in case someone else comes across a similar issue.

    Rediscovered after 8 months of using nhibernate that I’d forgotten a lot of SQL stuff 🙂

     DECLARE @IdCollectionCount         INT
        , @IdCollection             VARCHAR(250)
        , @CollectionDelimiter      NVARCHAR
    
        SET @IdCollectionCount = 2;
        SET @IdCollection = '1,4';
        SET @CollectionDelimiter= ',';
    
        WITH CustomQuery AS
            (
            SELECT sc.SpecialismComboId,
            count(s.SpecialismId) AS ItemCount
            FROM SpecialismCombo sc 
            inner join SpecialismComboSpecialism scs on sc.SpecialismComboId = scs.SpecialismComboId
            inner join Specialism s on s.SpecialismId = scs.SpecialismId
            GROUP BY sc.SpecialismComboId 
            HAVING count(s.SpecialismId) = @IdCollectionCount
            )
    
            SELECT Top 1 CustomQuery.SpecialismComboId FROM CustomQuery
            INNER JOIN SpecialismComboSpecialism scs on CustomQuery.SpecialismComboId = scs.SpecialismComboId
            INNER JOIN dbo.fn_SplitDelimited(@IdCollection,@CollectionDelimiter) AS ids
                  ON scs.SpecialismId = CAST(ids.ListValue AS INT)
            GROUP BY CustomQuery.SpecialismComboId 
            HAVING count(scs.SpecialismId) = @IdCollectionCount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entities Post and Tag , where there is a many-to-many relationship between
I have a many to many relationship between entities and there is a table
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I have a many-to-many relationship between User s and Task s. I want the
Is it possible to have a many to many relationship between two tables, and
I have a one-to-many relationship between two classes for this situation. I have a
I have a one-to-many relationship between Foo and Bar and I cannot perform an
android noob... I have two tables, with a one to many relationship between country_tbl
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag

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.