I’ve got a NHibernate query called in my C# code that returns an an Object Array which seems to work fine except when I try to iterate over the items in the array to use the values. When I try a foreach over them, I get an exception:
Cannot apply indexing with [] to an expression of type 'object'.
Here is a simplified version of the code:
var counts =
GetSession().CreateQuery(
@"select Name, count(Id) from
Account
group by Name")
.Enumerable();
foreach (var count in counts)
{
string s = count[0];
}
Can anyone help me figure out how to iterate over the results of the Enumerable returned?
Presumably the
Enumerablemethod is either just returningIEnumerable, orIEnumerable<object>. Either way, the implicit typing ofcountis giving youobjectwhich you presumably don’t want. I suspect you want something like: