Convert List<T> to ObservableCollection<T> in WP7
Solution 1
Apparently, your project is targeting Windows Phone 7.0. Unfortunately the constructors that accept IEnumerable<T>
or List<T>
are not available in WP 7.0, only the parameterless constructor. The other constructors are available in Silverlight 4 and above and WP 7.1 and above, just not in WP 7.0.
I guess your only option is to take your list and add the items into a new instance of an ObservableCollection
individually as there are no readily available methods to add them in bulk. Though that's not to stop you from putting this into an extension or static method yourself.
var list = new List<SomeType> { /* ... */ };
var oc = new ObservableCollection<SomeType>();
foreach (var item in list)
oc.Add(item);
But don't do this if you don't have to, if you're targeting framework that provides the overloads, then use them.
Solution 2
ObservableCollection has several constructors which have input parameter of List<T> or IEnumerable<T>:
List<T> list = new List<T>();
ObservableCollection<T> collection = new ObservableCollection<T>(list);
Solution 3
To convert List<T> list
to observable collection you may use following code:
var oc = new ObservableCollection<T>();
list.ForEach(x => oc.Add(x));
Solution 4
You'll have to write your own extension method to do this:
public static class CollectionEx
{
/// <summary>
/// Copies the contents of an IEnumerable list to an ObservableCollection
/// </summary>
/// <typeparam name="T">The type of objects in the source list</typeparam>
/// <param name="enumerableList">The source list to be converted</param>
/// <returns>An ObservableCollection containing the objects from the source list</returns>
public static ObservableCollection<T> ToObservableCollection<T>( this IEnumerable<T> enumerableList )
{
if( enumerableList != null ) {
// Create an emtpy observable collection object
var observableCollection = new ObservableCollection<T>();
// Loop through all the records and add to observable collection object
foreach( var item in enumerableList ) {
observableCollection.Add( item );
}
// Return the populated observable collection
return observableCollection;
}
return null;
}
}
Solution 5
ObservableCollection<FacebookUser_WallFeed> result = new ObservableCollection<FacebookUser_WallFeed>(FacebookHelper.facebookWallFeeds);
Comments
-
Josh Close almost 2 years
I don't know if it's just too late or what, but I don't see how to do this...
What I'm expecting to do, and what the object browser says is there, is this:
var oc = new ObservableCollection<T>( new List<T>() );
But
ObservableCollection<T>
has a single parameterless constructor. The object browser says there is 2 overloads where List and IEnuerable should be able to be passed in.Is there something wrong with my setup? Are the constructors not on the phone version? (that would be strange)
If this really doesn't exist, what is the standard way of doing this with WP7 now?
-
Nigel Sampson about 13 yearsAnother useful extension method is AddRange
-
Josh Close about 13 yearsNice to see the phone icons in MSDN in that link. Too bad the object browser can't figure out it's not available.
-
Josh Close about 13 yearsAgain, I'm surprised this is something that doesn't already exist in the framework. Pretty easy to implement, but is probably needed all over the place. I always seem to end up implementing this one too:
IEnumerable<TSource>.Each( Action<TSource, int> predicate )
-
Domenic about 13 years@Josh Close: with regards to your
Each
method, see blogs.msdn.com/b/ericlippert/archive/2009/05/18/… -
Josh Close about 13 years@Domenic: Thanks for the link! Lol:
Clearly the sole purpose of a call to this method is to cause side effects
. It's so true. I agree with his reason why it shouldn't be there. It is something that I do like to use though, but you do run into the possible closure issues. Same with Javascript though. -
Bastien Vandamme almost 12 yearsI prefer to stop coding than doing that.
-
Josh Close about 7 yearsNot in Windows Phone 7 it doesn't.
-
vitor_gaudencio_oliveira about 7 yearsPlease, look all the answers. There's no answer like mine. I feel bad for you.
-
Randika Vishman about 5 yearsUsed this today also (Apr 29, 2019) This should be the valid answer especially as its using the
ObservableCollection
given SDK API. @eugene.sushilnikov may earn the Populist badge ;-) -
Piero Alberto over 4 yearsUse this: ObservableCollection<SomeType> oc = new ObservableCollection<SomeType>(list );