AndySavage
AndySavage

Reputation: 1769

NHibernate querying with WHERE conditions in pairs

I have a collection of NHibernate objects ConcurrentBag<Event> with properties project_id and name. I want to a set of unrelated (schema wise) objects from another table which also match these properties.

The SQL I would expect would look something like:

SELECT * FROM table WHERE (project_id = 1 AND name = 'foo') 
    OR (project_id = 2 AND name = 'foo') 
    OR (project_id = 1 AND name = 'bar') 
    ... etc

The pairs of values in the WHERE clause are based on the values from each Event in the ConcurrentBag<Event>.

I am not sure how to query this with NHibernate (ideally with LINQ). Is this even possible?

Upvotes: 1

Views: 233

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

This would be difficult with LINQ I think, but if you use QueryOver it's easy to build a WHERE clause dynamically using Restrictions.Disjunction:

var disjunction = Restrictions.Disjunction();

foreach (Event evt in events)
{
    disjunction.Add(Restrictions.Where<Table>(
        t => t.project_id == evt.project_id && t.name == evt.name);
}

var rows = session.QueryOver<Table>()
    .Where(disjunction)
    .List<Table>();

Upvotes: 2

Related Questions