我有一个有EntitySet的模型。我正在尝试构建LINQ语句,但我不知道如何形成代码。我得到一个强制转换错误,因为它不能将泛型列表转换为EntitySet类型。
select new ParentRecord {
ParentID = item.ParentID,
Name = item.Name,
Age = item.Age,
MyNestedChildRecords = (from ns in item.MyNestChildRecords
select ns).ToList();
}.ToList();无法将源类型“列表”转换为目标类型"EntitySet"
发布于 2016-03-15 21:45:19
很明显,你有一种
from item in myItems
select new ParentRecord {
....问题是,您不能简单地将List<T>转换为EntitySet<T>,因为EntitySet<T>没有合适的构造函数。
最简单的方法是在Select中使用LINQ语法和匿名方法。
var result = myItems.Select(item =>
{
var record = new ParentRecord
{
ParentID = item.ParentID,
Name = item.Name,
Age = item.Age
};
record.MyNestedChildRecords.AddRange(item.MyNestChildRecords);
return record;
}).ToList()我假设ParentRecord是一个LINQ实体类,因此它的MyNestedChildRecords将被初始化。
发布于 2016-03-15 13:27:24
试试这个:
var test = (from ns in item.MyNestedChildRecords
select new ParentRecord
{
ParentID = item.ParentID,
Name = item.Name,
Age = item.Age,
MyNestedChildRecords = new EntitySet<MyNestedChildRecords>() { ns }
}).ToList();https://stackoverflow.com/questions/36012230
复制相似问题