【发布时间】:2020-04-17 20:05:30
【问题描述】:
如何修复无法将 System.Collections.Generic.List <RPHistory> 隐式转换为 System.Collections.Generic.List <RPHistory> 异常错误。
我正在尝试将两个实体组合在一起以获得一个列表
RP 实体类:
public class RP
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
}
RPHistory 实体类:
public class RPHistory:
{
public int ID { get; set; }
public int RPID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
}
我创建了第三个类
RpWithHistory 类:
public class RpWithHistory {
public int ID;
public int RPID;
public string Name;
public int ProductID;
public List<RPHistory> History;
}
Linq 查询
var RPs = await Context.RP.Where(b => b.ProductID == request.ID)
.Select(x=> new RpWithHistory {
ID = x.ID,
RPID = x.RPID,
Name = x.Name,
ProductID = x.ProductID,
History = Context.RPHistory
.Where(y=> y.RPID
== x.RPID)
.ToList()
}
).ToListAsync();
但我得到了这个错误,
>Cannot implicitly convert System.Collections.Generic.List <RPHistory> to
>System.Collections.Generic.List <RPHistory> exception error
谢谢!
【问题讨论】:
-
public List<RPHistory> History;是属性还是变量? -
@mvermef 感谢它的属性
-
你确定这两个 RPHistory 类型真的是相同的类型吗?如果您右键单击类型并选择“转到定义”,它们是否指向同一个地方?如果是这样,我不明白为什么会显示任何错误
-
RP 和 RPHistory 相同的事实本身就很奇怪......
标签: c# asp.net asp.net-mvc linq