问题描述
你会如何转换或蒙上了列表< T>
到 EntityCollection< T>
有时,这种试图从零开始的子对象(例如,从Web表单)
的集合创建时出现无法隐式转换类型
System.Collections.Generic.List来
System.Data.Objects.DataClasses.EntityCollection
我假设你正在谈论列表< T>
和 EntityCollection< T&GT ;
这是使用的实体框架。因为后者有一个完全不同的目的(它负责更改跟踪),并没有继承列表< T>
,也没有直接的投
您可以创建一个新的 EntityCollection< T>
并添加所有的成员
VAR entityCollection =新EntityCollection< TEntity>();
的foreach(列表中的项目变种M)
{
entityCollection.Add(米);
}
不幸的是 EntityCollection< T>
既不支持一个指定操作并使用LINQ2SQL EntitySet的,也不是一个重载的构造函数,这样就是你离开的地方与我上述
How would you to convert or cast a List<T>
to EntityCollection<T>
?
Sometimes this occurs when trying to create 'from scratch' a collection of child objects (e.g. from a web form)
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Data.Objects.DataClasses.EntityCollection'
I assume you are talking about List<T>
and EntityCollection<T>
which is used by the Entity Framework. Since the latter has a completely different purpose (it's responsible for change tracking) and does not inherit List<T>
, there's no direct cast.
You can create a new EntityCollection<T>
and add all the List members.
var entityCollection = new EntityCollection<TEntity>();
foreach (var item m in list)
{
entityCollection.Add(m);
}
Unfortunately EntityCollection<T>
neither supports an Assign operation as does EntitySet used by Linq2Sql nor an overloaded constructor so that's where you're left with what I stated above.
这篇关于转换或投下名单,LT; T&GT;到EntityCollection&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!