流畅的Nhibernate计数(*)函数出错

时间:2011-10-21 12:59:22

标签: wcf nhibernate

我必须根据标准检索一些数据和行数 下面是我的代码片段

 criteria.SetProjection(Projections.ProjectionList().Add(Projections.Property("LastUpdatedUserName"),"OperatorName")
            .Add(Projections.Property("Created"),"enrollmentdate")
            .Add(Projections.Count("NIK"), "enrollmentcount")
            .Add(Projections.GroupProperty("LastUpdatedUserName"))
            .Add(Projections.GroupProperty("Created")))
            .SetResultTransformer(NHibernate.Transform.Transformers.AliasToEntityMap);
       var result = criteria.List<Demographic>()

此代码段在运行时导致异常

这里是例外

ex.message = Unable to perform find[SQL: SQL not available] ex.innerexception = {"The value \"System.Collections.Hashtable\" is not of type \"Indo.Id.Data.Infrastructure.Entities.Demographic\" and cannot be used in this generic collection.\r\nParameter name: value"}

并且堆栈跟踪是

at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType) at System.Collections.Generic.List 1.VerifyValueType(Object value)    在System.Collections.Generic.List 1.System.Collections.IList.Add(Object item) at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Util\ArrayHelper.cs:line 233 at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1948

转换为人口统计的类型转换将最大程度地工作但是在演示图形中我有大约40列,并且我已经声明了一个新类来保存结果,如

public class operatorenrollment { public string OperatorName { get; set; } public DateTime enrollmentdate { get; set; } public int enrollmentcount { get; set; } }

我现在可以将其转换为新类,如

NHibernate.Transform.Transformers.AliasToBean(typeof(operatorenrollment))

这里的任何帮助都是非常有用的

感谢adv

1 个答案:

答案 0 :(得分:0)

错误很清楚。

您正在使用Transformers.AliasToEntityMap,它会将您的投影转换为IDictionary,并尝试返回Demographic列表。

改为使用Transformers.AliasToBean<Demographic>()