转换或投下名单,LT; T>到EntityCollection< T> [英] convert or cast a List<t> to EntityCollection<T>

查看:154
本文介绍了转换或投下名单,LT; T>到EntityCollection< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会如何转换或蒙上了列表< 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&LT; T&GT; 既不支持一个指定操作并使用LINQ2SQL EntitySet的,也不是一个重载的构造函数,这样就是你离开的地方与我上述<。 / p>

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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆