[英]Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List
I am new to LINQ I am getting this error 我是LINQ的新手,出现此错误
Cannot implicitly convert type
System.Collections.Generic.List<AnonymousType#1>
toSystem.Collections.Generic.List<DirCert.Data.Model.DssClient_Sasid_Certified>
无法将类型System.Collections.Generic.List<AnonymousType#1>
隐式转换为System.Collections.Generic.List<DirCert.Data.Model.DssClient_Sasid_Certified>
I can't figure out how to solve this error. 我不知道如何解决此错误。
Here is my code: 这是我的代码:
public List<DssClient_Sasid_Certified> GetCertifiedRecordsbySasid(string Sasid)
{
return (from o in _context.DssClient_Sasid_Certified
where (o.SasId == Sasid)
join t in _context.DssClients on o.ClientId equals t.ClientId
select new
{
ClientId = o.ClientId,
SasId = o.SasId,
FormalLastName = o.FormalLastName,
FormalFirstName = o.FormalFirstName,
FormalMiddleName = o.FormalMiddleName,
BenefitSource = t.BenefitSource,
DOB = o.DOB
}).ToList();
}
Your select clause: 您的选择子句:
select new
{
...
}
is selecting an anonymous type. 正在选择匿名类型。 You need it to select a DssClient_Sasid_Certified
, so that you can return the type that your method declaration says you're going to return. 您需要它来选择DssClient_Sasid_Certified
,以便您可以返回方法声明表明您将要返回的类型。 You may just need to change your code to: 您可能只需要将代码更改为:
select new DssClient_Sasid_Certified
{
...
}
... assuming DssClient_Sasid_Certified
has all the properties you're setting, of course. ...当然,假设DssClient_Sasid_Certified
具有您要设置的所有属性。
Your method is supposed to be returning a List of DssClient_Sasid_Certified, but your linq query is creating an anon type. 您的方法应该返回DssClient_Sasid_Certified列表,但是您的linq查询正在创建匿名类型。
You could do something like: 您可以执行以下操作:
public List<DssClient_Sasid_Certified> GetCertifiedRecordsbySasid(string Sasid)
{
return (from o in _context.DssClient_Sasid_Certified
where (o.SasId == Sasid)
join t in _context.DssClients
on o.ClientId equals t.ClientId
select new DssClient_Sasid_Certified()
{
ClientId = o.ClientId,
SasId = o.SasId,
FormalLastName = o.FormalLastName,
FormalFirstName = o.FormalFirstName,
FormalMiddleName = o.FormalMiddleName,
BenefitSource = t.BenefitSource,
DOB = o.DOB
}).ToList();
}
This is of course assuming your object has a parameterless constructor for DssClient_Sasid_Certified, and that the properties you're using in your anon object are the same as your DssClient_Sasid_Certified 当然,这是假设您的对象具有DssClient_Sasid_Certified的无参数构造函数,并且您在anon对象中使用的属性与DssClient_Sasid_Certified相同
The line from your original query: 来自原始查询的行:
select new .....
Basically says, create me a new Anonymous type, which an anonymous type cannot be converted automagically to a non anonymous type (your list of DssClient_Sasid_Certified). 基本上说,为我创建一个新的匿名类型,该匿名类型不能自动转换为非匿名类型(您的DssClient_Sasid_Certified列表)。
Read more about Anonymous types here: http://msdn.microsoft.com/en-us/library/bb397696.aspx 在这里阅读有关匿名类型的更多信息: http : //msdn.microsoft.com/en-us/library/bb397696.aspx
Error message states it clearly.Your return type is List<DssClient_Sasid_Certified>
but you are trying to return a list of anonymous types.So the types do not match,they are not compatible. 错误消息清楚地指出了这一点。您的返回类型为List<DssClient_Sasid_Certified>
但是您尝试返回的是匿名类型的列表。因此,这些类型不匹配,因此不兼容。 You need to create instances of your type instead of anonymous types: 您需要创建您类型的实例,而不是匿名类型的实例:
select new DssClient_Sasid_Certified { .. }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.