无法将类型'System.Collections.Generic.List转换为'System.Collections.Generic.List [英] Cannot convert type 'System.Collections.Generic.List to 'System.Collections.Generic.List
本文介绍了无法将类型'System.Collections.Generic.List转换为'System.Collections.Generic.List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要帮助,我编写了一项从数据库表中获取记录的服务.其返回类型为List<>.我正在使用控制器方法调用该服务.
但这给了我铸造错误.我的代码如下:
这就是服务:
Hi all,
i need help, i have written a service which is fetching records from database table. its return type is List<> and i am calling the service in a controller method.
but it is giving me casting error. My code is below:
this is the service :
public List<tblClaimRegistration> GetAllClaimRegistrationRecords()
{
MACPEntities entities;
entities = new MACPEntities();
IEnumerable<tblClaimRegistration> records = from obj in entities.tblClaimRegistrations
select obj;
return records.ToList();
}
这是控制器方法:
this is the controller method:
[HttpPost]
public ActionResult ToControl()
{
ServiceReference1.Service1Client svc = new ServiceReference1.Service1Client();
List<tblClaimRegistration> ClaimRegistartion= (List<tblClaimRegistration>)svc.GetAllClaimRegistrationRecords().ToList();
ViewData["ClaimRegistartion"] = ClaimRegistartion;
return View("TestView");
}
上面的代码给出了错误
the above code is giving error as
Cannot convert type ''System.Collections.Generic.List to ''System.Collections.Generic.List<MACP.Models.tblClaimRegistration>'' C:\inube\MACPCMSIntegration\MACP\Controllers\HomeController.cs 127 60 MACP
pls some body help what should i do.
推荐答案
方法"GetAllClaimRegistrationRecords()"已经返回"List< tblclaimregistration>".类型.
因此,您不需要询问ToList()...
那就做吧:
The method "GetAllClaimRegistrationRecords()" already return a "List<tblclaimregistration>" type.
So you doesn''t need to ask for ToList()...
Just do it :
List<tblClaimRegistration> ClaimRegistartion= svc.GetAllClaimRegistrationRecords()
我得到了解决方案:
i got the solution :
[HttpPost]
public ActionResult ToControl()
{
ServiceReference1.Service1Client svc = new ServiceReference1.Service1Client();
ViewData["ClaimRegistartion"] = svc.GetAllClaimRegistrationRecords();
return View("TestView");
}
它将起作用.
it will work.
这篇关于无法将类型'System.Collections.Generic.List转换为'System.Collections.Generic.List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文