Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic List to EntitySet Conversion

Tags:

c#

linq

entityset

How do I Convert a System.Collections.Generic.List<T> to a System.Data.Linq.EntitySet<T> ?

like image 592
Johnny Avatar asked May 17 '10 11:05

Johnny


2 Answers

Don't think you can convert a List<T> to an EntitySet<T> but you can put the content of your list in the entitySet.

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

Here's a extension method for that:

public static EntitySet<T> ToEntitySet<T>(this IEnumerable<T> source) where T : class
{
    var es = new EntitySet<T>();
    es.AddRange(source);
    return es;
}
like image 199
Julien Hoarau Avatar answered Sep 22 '22 08:09

Julien Hoarau


var list = new List<string> 
{ 
    "element1", "element2" 
};
var entitySet = new EntitySet<string>();
entitySet.AddRange(list);
like image 29
Darin Dimitrov Avatar answered Sep 25 '22 08:09

Darin Dimitrov