无法将带有[]的索引应用于从NHibernate返回的Object Array的表达式类型'object'

Rus*_*ark 2 c# iteration nhibernate

我有一个在我的C#代码中调用的NHibernate查询返回一个似乎工作正常的对象数组,除非我尝试迭代数组中的项目以使用这些值.当我尝试使用foreach时,我得到一个例外:

Cannot apply indexing with [] to an expression of type 'object'.

这是代码的简化版本:

        var counts =
            GetSession().CreateQuery(
                @"select Name, count(Id) from
                                    Account
                                    group by Name")
                .Enumerable();

        foreach (var count in counts)
        {
            string s = count[0];
        }
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我弄清楚如何迭代返回的Enumerable的结果?

Jon*_*eet 5

据推测,该Enumerable方法要么就是返回IEnumerable,要么就是IEnumerable<object>.无论哪种方式,隐式输入count都会给你object你可能不想要的东西.我怀疑你想要的东西:

// Giving count an explicit type tells the compiler to insert a cast.
// Obviously this will fail if it's *not* an array of some reference type.
foreach (object[] count in counts)
{
    string s = (string) count[0];
}
Run Code Online (Sandbox Code Playgroud)