I'm trying to perform a simple aggregate query that returns the aggregate's result plus an extra column. This post -> Custom query with Castle ActiveRecord had a good example about how to achieve this, but I can't seem to get it to work. It seems that ActiveRecordMediator.ExecuteQuery returns an ArrayList of objects (instead of ArrayList of object[] which is what I would expect). Also if I try to cast it to ICollection I get a run-time error complaining of invalid cast. Code below, any help appreciated (don't want to use hand-written sql).
HqlBasedQuery query = new HqlBasedQuery(typeof(Something), @"select count(1),
p.Name from Something p
where p.SomeDate > :date
order by p.Name
group by p.Name");
query.SetParameter("date", new DateTime(2009, 1, 1));
var results = from summary in
(ICollection<object[]>)ActiveRecordMediator.ExecuteQuery(query)
select new {
Count = (int)summary[0], Name= (string)summary[1]
};
The line after "from summary in" is the one that throws the invalid cast exception.
(Forgot to mention: using VS2008, .NET 3.5SP1, ActiveRecord 1.0RC3, NHibernate 1.2)
count(*)
instead of count(1)
(this is why you're getting only 1-col rows)ArrayList
(not a generic ICollection
) of object[]
count
results as int. Some databases return counts as long
(e.g. SQL Server)