通用列表到EntitySet转换

时间:2010-05-17 11:43:16

标签: c# linq entityset

如何将System.Collections.Generic.List<T>转换为System.Data.Linq.EntitySet<T>

2 个答案:

答案 0 :(得分:33)

不要认为您可以将List<T>转换为EntitySet<T>,但您可以将列表的内容放在entitySet中。

var list = new List<string> { "a", "b", "c" };
var entitySet = new EntitySet<string>();
entitySet.AddRange(list);

这是一个扩展方法:

public static EntitySet<T> ToEntitySet<T>(this IEnumerable<T> source) where T : class
{
    var es = new EntitySet<T>();
    es.AddRange(source);
    return es;
}

答案 1 :(得分:11)

var list = new List<string> 
{ 
    "element1", "element2" 
};
var entitySet = new EntitySet<string>();
entitySet.AddRange(list);
相关问题