简体   繁体   English

错误:值“ System.Object []”不是类型“ prjCustomerService.clsCustomer”,因此不能在此通用集合中使用。 参数名称:值

[英]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": 就我而言,我用来在QueryOverselectalias与我实际“选择为”的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.

相关问题 值“CitiesDomain.Cities”不是“CitiesDomain.Cities”类型,不能在此通用集合中使用。 参数名称:值 - The value “CitiesDomain.Cities” is not of type “CitiesDomain.Cities” and cannot be used in this generic collection. Parameter name: value 为什么 CreateSQLQuery 抛出异常“值 System.Object[] 不是类型,不能在此泛型集合中使用”? - Why CreateSQLQuery throwing exception "The value System.Object[] is not of type and cannot be used in this generic collection"? 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 无法在实体框架中创建类型为“System.Object”的常量值 - Unable to create a constant value of type 'System.Object' in Entity Framework Linq to Entities-“无法创建类型&#39;System.Object&#39;的常量值...” - Linq to Entities - “Unable to create a constant value of type 'System.Object'…” 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM