[英]Error:The value “System.Object[]” is not of type “prjCustomerService.clsCustomer” and cannot be used in this generic collection. Parameter name: value
public IList<CustomerData> StoredProcedure(int CID)
{
IList<CustomerData> lObjEmployees = new List<CustomerData>();
NHibernate.Cfg.Configuration lObjConfig = new NHibernate.Cfg.Configuration();
lObjConfig.AddAssembly("prjCustomerService");
NHibernate.ISessionFactory lObjFactory = lObjConfig.BuildSessionFactory();
NHibernate.ISession lObjSession = lObjFactory.OpenSession();
NHibernate.IQuery lObjQuery = lObjSession.GetNamedQuery("Test1");
lObjQuery.SetInt32("CID", @CID);
IList<clsCustomer> lObjEmpList = lObjQuery.List<clsCustomer>();
foreach (clsCustomer lObjCurrEmployee in lObjEmpList)
{
CustomerData lObjCurrEmpData = new CustomerData();
lObjCurrEmpData.CID = lObjCurrEmployee.CID;
lObjCurrEmpData.CName = lObjCurrEmployee.CName;
lObjCurrEmpData.Cadd = lObjCurrEmployee.CAdd;
lObjCurrEmpData.Ccontact = lObjCurrEmployee.CContact;
lObjEmployees.Add(lObjCurrEmpData);
}
return lObjEmployees;
}
This is my WCF service code.I get an error at line: 这是我的WCF服务代码,在以下行出现错误:
IList<clsCustomer> lObjEmpList = lObjQuery.List<clsCustomer>();
The value "System.Object[]" is not of type "prjCustomerService.clsCustomer" and cannot be used in this generic collection.
值“ System.Object []”的类型不是“ prjCustomerService.clsCustomer”,因此不能在此通用集合中使用。
Parameter name: value
参数名称:值
Test1
is my stored procedure and it returns a row where CID="xyz"
. Test1
是我的存储过程,它返回一行CID="xyz"
。
In my case, the alias
that I was using to select
in the QueryOver
was different from what I was actually "selecting as": 就我而言,我用来在
QueryOver
中select
的alias
与我实际“选择为”的alias
不同:
The Query (notice the with alias
): 查询(注意
with alias
):
IndividualBugViewModel individualBugViewModelAlias = null;
IQueryOver<DomainModel.Models.Bug, DomainModel.Models.Bug> bugQueryOver = Repository
.QueryOver(() => bugAlias)
.SelectList(list => list.SelectGroup(() => bugAlias.Details).WithAlias(
() => individualBugViewModelAlias.Details)
The Result: 结果:
var bugList = bugQueryOver.List() //Error!
Because, when running the query, I was not passing the class type into which the result should be transformed to, it would try to transform the result into type DomainModel.Models.Bug
instead of IndividualBugViewModel
. 因为在运行查询时,我没有传递将结果转换为的类类型,所以它将尝试将结果转换为
DomainModel.Models.Bug
类型,而不是IndividualBugViewModel
类型。 This resulted into the error. 这导致了错误。
So I changed it to: 所以我将其更改为:
var bugList = bugQueryOver.TransformUsing(Transformers.AliasToBean<IndividualBugViewModel>())
.List<IndividualBugViewModel>();
In your case, the <return class="className">
in your named query
must be incompatible with clsCustomer
. 在您的情况下,
named query
的<return class="className">
必须与clsCustomer
不兼容。
Try: 尝试:
IList<clsCustomer> lObjEmpList = lObjQuery.OfType<clsCustomer>.ToList();
Remember to add using System.Linq
to your using statements. 记住要在您的using语句中添加
using System.Linq
。
With this statement your say that your non-generic IQuery must be filtered to contain only clsCustomer
objects, then with .ToList you say that you want to create a list out of it. 使用此语句,您说必须将非通用IQuery过滤为仅包含
clsCustomer
对象,然后使用.ToList表示要从中创建一个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.