I'm using NHibernate and I have a query which I want to run, which involves returning the whole table plus a count and group by. From what I've read online you can't do this with NHibernate Criteria.
To get round this, I'm using a named query:
SELECT id, COUNT(id) AS myCount
FROM foo INNER JOIN bah
ON foo.id = bah.fooId
WHERE foo.Name LIKE :name
GROUP BY fooId
this works fine when I only want to pass in one parameter.
What I actually want to achieve though is:
SELECT id, COUNT(id) AS myCount FROM
foo INNER JOIN bah ON foo.id = bah.fooId
WHERE foo.Name LIKE :name1
OR foo.Name LIKE :name2
OR foo.Name LIKE :name3
GROUP BY fooId
the number of parameters is changeable - so there could be 2 or 3 or 4 names that I want to match.
I got round it using a criteria with the following code:
ICriteria criteria = session.CreateCriteria(typeof (Foo)).CreateCriteria("Bah");
Disjunction disjunction = Restrictions.Disjunction();
foreach (Foo foo in fooToFind)
{
disjunction.Add(Restrictions.Like("Name", "%" + Foo.Name + "%"));
}
criteria.Add(disjunction);
result = criteria.List();
(like I said above though it doesn't look like I can use this because I can't return a count as part of the return)
So I tried using query.SetParameterList:
IQuery query = session.GetNamedQuery("myNamedQuery");
query.SetParameterList("name", new string[] {"eeeny", "meeny"});
however this seems to gives me the sql:
SELECT id, COUNT(id) AS myCount
FROM foo INNER JOIN bah
ON foo.id = bah.fooId
WHERE foo.Name LIKE 'eeny','meeny'
GROUP BY food
so nHibernate doesn't like the comma.
Changing the query to
SELECT id, COUNT(id) AS myCount
FROM foo INNER JOIN bah
ON foo.id = bah.fooId
WHERE foo.Name IN (:name)
GROUP BY fooId
works well, but doesn't give me the results I want.
Does anyone know how I can achieve this result - using any nhibernate method?
SELECT id, COUNT(id) AS myCount FROM
foo INNER JOIN bah ON foo.id = bah.fooId
WHERE foo.Name LIKE :name1
OR foo.Name LIKE :name2
OR foo.Name LIKE :name3
GROUP BY fooId
Thanks!