无法投射类型' CRUD_XML_MVC.Models.Billing'键入' System.Collections.Generic.IEnumerable

时间:2014-06-03 22:53:52

标签: c# .net asp.net-mvc-4

在我的MVC4应用程序中,我使用以下代码:

   Billing WCFBill = new Billing();
   WCFBill.ID = XRec.ID;
   WCFBill.Customer = XRec.Customer;
   WCFBill.Date = XRec.Date;
   WCFBill.Description = XRec.Description;
   WCFBill.Type = XRec.Type;
   WCFBill.Hours = XRec.Hours;
>  var result = ((IEnumerable<Billing>)WCFBill).Cast<Billing>().ToList();
   List<Billing> WCFBillList = new List<Billing>();
   WCFBillList.AddRange(result);

上面指出的行引发了以下错误:

  

无法投射类型为&#39; CRUD_XML_MVC.Models.Billing&#39;输入&#39; System.Collections.Generic.IEnumerable`1 [CRUD_XML_MVC.Models.Billing]&#39;

2 个答案:

答案 0 :(得分:1)

首先,这告诉我们WCFBill的类型为Billing

Billing WCFBill = new Billing();

然后这一行(相关部分剪辑)说你试图将Billing对象转换为IEnumerable<Billing>类型,这是不可能的:

(IEnumerable<Billing>)WCFBill

答案 1 :(得分:0)

WCFBillBilling的单个实例,您正在尝试将其转换为集合。

替换最后3行:

var result = ((IEnumerable<Billing>)WCFBill).Cast<Billing>().ToList();
List<Billing> WCFBillList = new List<Billing>();
WCFBillList.AddRange(result);

有了这个:

var WCFBillList = new List<Billing> { WCFBill };