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?
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);
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.
To convert List<T> list to observable collection you may use following code:
var oc = new ObservableCollection<T>();
list.ForEach(x => oc.Add(x));
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;
}
}
Extension method from this answer IList<T> to ObservableCollection<T> works pretty well
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable) {
var col = new ObservableCollection<T>();
foreach ( var cur in enumerable ) {
col.Add(cur);
}
return col;
}
ObservableCollection<FacebookUser_WallFeed> result = new ObservableCollection<FacebookUser_WallFeed>(FacebookHelper.facebookWallFeeds);
Use this:
List<Class1> myList;
ObservableCollection<Class1> myOC = new ObservableCollection<Class1>(myList);
If you are going to be adding lots of items, consider deriving your own class from ObservableCollection and adding items to the protected Items member - this won't raise events in observers. When you are done you can raise the appropriate events:
public class BulkUpdateObservableCollection<T> : ObservableCollection<T>
{
public void AddRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Add(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
}
}
When adding many items to an ObservableCollection that is already bound to a UI element (such as LongListSelector) this can make a massive performance difference.
Prior to adding the items, you could also ensure you have enough space, so that the list isn't continually being expanded by implementing this method in the BulkObservableCollection class and calling it prior to calling AddRange:
public void IncreaseCapacity(int increment)
{
var itemsList = (List<T>)Items;
var total = itemsList.Count + increment;
if (itemsList.Capacity < total)
{
itemsList.Capacity = total;
}
}
I made an extension so now I can just load a collection with a list by doing:
MyObservableCollection.Load(MyList);
The extension is:
public static class ObservableCollectionExtension
{
public static ObservableCollection<T> Load<T>(this ObservableCollection<T> Collection, List<T> Source)
{
Collection.Clear();
Source.ForEach(x => Collection.Add(x));
return Collection;
}
}
The answer provided by Zin Min solved my problem with a single line of code. Excellent!
I was having the same issue of converting a generic List to a generic ObservableCollection to use the values from my List to populate a ComboBox that is participating in binding via a factory class for a WPF Window.
_expediteStatuses = new ObservableCollection<ExpediteStatus>(_db.getExpediteStatuses());
Here is the signature for the getExpediteStatuses method:
public List<ExpediteStatus> getExpediteStatuses()
Related
Lets say i have collection IList<Items> items = GetItems();
I want to convert that collection in to collection of MyDbSet<Items>
MyDbSet declared like that: public class MyDbSet<T> : IDbSet<T> where T : class
How can i do that with the less amount of code, possibly it is better to go with linq?
For collections that support ICollection<T>.Add you could write a helper method like this:
public static TCollection CreateCollection<TCollection,TElement>(IEnumerable<TElement> seq)
where TCollection:ICollection<TElement>, new
{
var coll=new TCollection();
foreach(var element in seq)
coll.Add(element);
return coll;
}
which is used like:
var coll = CreateCollection<MyDbSet<T>, T>(seq);
But personally I'd rather write an AddRange(seq) extension method and then use it like:
var coll = new MyDbSet<T>();
coll.AddRange(seq);
I don't think there is a way other than just loop over the source collection and call Add on your MyDbSet class instance, however it can be hidden in custom extension method:
public static class MyEnumerable
{
public static MyDbSet<T> ToMyDbSet<T>(this IEnumerable<T> source)
where T : class
{
var collection = new MyDbSet<T>();
foreach (var item in source)
collection.Add(item);
return collection;
}
}
Usage:
IList<Items> items = GetItems();
MyDbSet<Items> itemsSet = items.ToMyDbSet();
I am looking for Linq way (like RemoveAll method for List) which can remove selected items from my ObservableCollection.
I am too new to create an extension method for myself. Is there any way I remove items from ObservableCollection passing a Lambda expression?
I am not aware of a way to remove only the selected items. But creating an extension method is straight forward:
public static class ExtensionMethods
{
public static int Remove<T>(
this ObservableCollection<T> coll, Func<T, bool> condition)
{
var itemsToRemove = coll.Where(condition).ToList();
foreach (var itemToRemove in itemsToRemove)
{
coll.Remove(itemToRemove);
}
return itemsToRemove.Count;
}
}
This removes all items from the ObservableCollection that match the condition. You can call it like that:
var c = new ObservableCollection<SelectableItem>();
c.Remove(x => x.IsSelected);
Iterating backwards should be more efficient than creating a temporary collection as in Daniel Hilgarth's example.
public static class ObservableCollectionExtensions
{
public static void RemoveAll<T>(this ObservableCollection<T> collection,
Func<T, bool> condition)
{
for (int i = collection.Count - 1; i >= 0; i--)
{
if (condition(collection[i]))
{
collection.RemoveAt(i);
}
}
}
}
How about this implementation for a one-liner?
observableCollection.Where(l => l.type == invalid).ToList().All(i => observableCollection.Remove(i))
-- Edit --
Sorry, yes, you need a ToList() in the middle to force the first half to evaluate, as LINQ does lazy evaluation by default.
Each of solution proposed here which uses routine to remove item one by one has one fault. Imagine that you have many items in observable collection, lets say 10.000 items. Then you want to remove items which meets some condition.
If you use solution from Daniel Hilgarth and call: c.Remove(x => x.IsSelected); and there are for example 3000 items to be removed, proposed solution will notify about each item removal. This is due to fact that internal implementation of Remove(item) notify about that change. And this will be called for each of 3000 items in removal process.
So instead of this i created descendant of ObservableCollection and add new method RemoveAll(predicate)
[Serializable]
public class ObservableCollectionExt<T> : ObservableCollection<T>
{
public void RemoveAll(Predicate<T> predicate)
{
CheckReentrancy();
List<T> itemsToRemove = Items.Where(x => predicate(x)).ToList();
itemsToRemove.ForEach(item => Items.Remove(item));
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
Interesting line is itemsToRemove.ForEach(item => Items.Remove(item));. Calling directly Items.Remove(item) will not notify about item removed.
Instead after removal of required items, changes are notified at once by calls:
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
This is my version of an extension method solution, which is only a slight variation on the accepted answer, but has the advantage that the count returned is based on confirmed removal of the item from the collection:
public static class ObservableCollectionExtensionMethods
{
/// <summary>
/// Extends ObservableCollection adding a RemoveAll method to remove elements based on a boolean condition function
/// </summary>
/// <typeparam name="T">The type contained by the collection</typeparam>
/// <param name="observableCollection">The ObservableCollection</param>
/// <param name="condition">A function that evaluates to true for elements that should be removed</param>
/// <returns>The number of elements removed</returns>
public static int RemoveAll<T>(this ObservableCollection<T> observableCollection, Func<T, bool> condition)
{
// Find all elements satisfying the condition, i.e. that will be removed
var toRemove = observableCollection
.Where(condition)
.ToList();
// Remove the elements from the original collection, using the Count method to iterate through the list,
// incrementing the count whenever there's a successful removal
return toRemove.Count(observableCollection.Remove);
}
}
There is no way to pass an expression to the ObservableCollection to remove matching items, in the same way that a generic list has. ObservableCollection adds and removes one item at a time.
You will have to create your own implementation of INotifyCollectionChanged in order to do this, or as you mention create an extension method.
ObservableCollection<AppVariable<G>> _appVariables = new new ObservableCollection<AppVariable<G>>();
var temp = AppRepository.AppVariables.Where(i => i.IsChecked == true).OrderByDescending(k=>k.Index);
foreach (var i in temp)
{
AppRepository.AppVariables.RemoveAt(i.Index);
}
Kinda late but just posting this up here since I couldn't find another solution online while I ran into this same issue:
obj objToRemove = Collection.First(obj => obj.ID == ID);
Collection.Remove(objToRemove);
Assuming u have an ID or Name of sorts where u can get your desired object to remove, you can use the '.First' method from ObservableCollections to get your object to remove and call the '.Remove' method to remove the selected item.
Alternatively, you can just throw the entire first line into the Remove method.
Collection.Remove(Collection.First(obj => obj.ID == ID));
I want to add colections to List, but only if Advertisements does not already exist in it. I know that HashSet works like this that has no duplicate values, but with HashSet i can not use AddRange and GetRange.
So is it possible to simulate List like hashSet?
List<Advertisements> advertisements = new List<Advertisements>();
advertisements.AddRange(NHibernateSession.CreateCriteria<Advertisements>()
.CreateCriteria(AdvertisementsProperties.City.ToString())
.Add(Restrictions.Or(
Restrictions.Like(CitiesProperties.Name.ToString(), text, MatchMode.Anywhere),
Restrictions.Like(CitiesProperties.SlovenianName.ToString(), text, MatchMode.Anywhere)))
.List<Advertisements>());
advertisements.AddRange(NHibernateSession.CreateCriteria<Advertisements>()
.CreateCriteria(AdvertisementsProperties.Area.ToString())
.Add(Restrictions.Or(
Restrictions.Like(AreasProperties.Name.ToString(), text, MatchMode.Anywhere),
Restrictions.Like(AreasProperties.SlovenianName.ToString(), text, MatchMode.Anywhere)))
.List<Advertisements>());
To add a bunch of items to a HashSet like AddRange does simply use:
set.UnionWith(items);
The items in a HashSet are not indexed (it's implemented as a hash table which is not designed for index based access to elements). If you strictly need to store items by index, you'll have to use a simple list and create your own Add method that checks Contains on each element before adding it to the underlying list. Of course, a linear list doesn't provide the efficiency of set operations as HashSet does.
Check out this post. The poster used an extension method to add "AddRange" functionality to HashSet.
You'd probably want to create your own class if you often need a hybrid List/Set collection functionality. It would go something like this:
using System;
using System.Collections;
using System.Collections.Generic;
public class ListSet<T> : IList<T>, ISet<T> // ISet<T> is supported only from .NET 4.0 on
{
#region Inner collections
private HashSet<T> _innerSet = new HashSet<T>();
private List<T> _innerList = new List<T>();
#endregion
#region The read methods delegate to the inner collection which is more appropriate and efficient:
public bool Contains(T item)
{
return this._innerSet.Contains(item);
}
public int IndexOf(T item)
{
return this._innerList.IndexOf(item);
}
// TODO: Implement all other read operations
#endregion
#region The write methods must keep both inner collections synchronized
public bool Add(T item)
{
bool wasAdded = this._innerSet.Add(item);
if (wasAdded) this._innerList.Add(item);
return wasAdded;
}
public void AddRange(IEnumerable<T> items)
{
foreach (T item in items) this.Add(item);
}
public bool Remove(T item)
{
if (this._innerSet.Remove(item))
{
return this._innerList.Remove(item);
}
return false;
}
// TODO: Implement all other write operations
// TODO: Consider implementing roll back mechanisms in exception handlers
// when write operations fail
#endregion
}
This is a compiler error (slightly changed for readability).
This one always puzzled me. FxCop tells that this is a bad thing to return List<T> and classes that are derived from Collection<T> should be preferable as return types.
Also, FxCop says that it is OK to use List<T> for internal data storage/manipulation. Ok, I get it, but what I don't get is why the compiler complains about trying to implicitly convert List<T> to Collection<T>. Isn't List<T> more interface-charged and functional? Why prohibit implicit conversion?
And another question that stems from above: is the new List<int>(some collection<int>) constructor expensive?
Why not just do the following:
Collection<string> collection = new Collection<string>(theList);
as Collection(IList input) takes a List as part of construction.
List<T> doesn't derive from Collection<T> - it does, however, implement ICollection<T>. That would be a better choice of return type.
As for the new List<int>(some collection<int>) question - it partly depends on what the collection is. If it implements ICollection<T> (at execution time) then the constructor can use its Count property to create the list with the right initial capacity before iterating through it and adding each item. If it doesn't implement ICollection<T> then it's just equivalent to:
List<int> list = new List<int>();
foreach (int x in otherCollection)
{
list.Add(x);
}
Still nice to have in a convenient constructor, but not hugely efficient - it can't be, really.
I don't believe the constructor does anything cunning for arrays, which it potentially could - using Array.Copy or whatever to just copy the lot in one go rather than iterating though. (Likewise if it were another List<T> it could get at the backing array and copy that directly.)
List<T> doesn't inherit from Collection<T>. Plain and simple. Unless List<T> provides an operator to implicitly convert to/from Collection<T>, you can't do it. I would actually suggest returning List<T> if you can, as I believe the rules go something like this:
Accept as a parameter the least constrictive interface possible.
Return as a return parameter the most constrictive type possible.
Here is a generic extension method written in C# 3.0 used to convert List<T> to Collection<T>
using System.Collections.Generic;
using System.Collections.ObjectModel;
public static class ExtensionMethods
{
public static Collection<T> ToCollection<T>(this List<T> items)
{
Collection<T> collection = new Collection<T>();
for (int i = 0; i < items.Count; i++)
{
collection.Add(items[i]);
}
return collection;
}
}
and it is used like this…
List<string> entities = new List<string>();
entities.Add("Value 1");
entities.Add("Value 2");
entities.Add("Value 3");
entities.Add("Value 4");
Collection<string> convertedEntities = entities.ToCollection<string>();
This is how you convert from List<T> to Collection<T> (while using LINQ):
The old function:
public List<Employee> GetEmployee(int id)
{
return ( from e in MyDataContext.Employees
select new Employee()
{
e.empId = id
}
).ToList();
}
After conversion:
using System.Collection.ObjectModel;
public Collection<Employee> GetEmployee(int id)
{
return new Collection<Employee>(
(from e in MyDataContext.Employees
select new Employee()
{
e.empId = id
}
).ToList() as IList<Employee>
);
}
You can use the the below
public class EmployeeCollection : Collection<Employee>
{
public EmployeeCollection(IList<Employee> list) : base(list)
{}
public EmployeeCollection() : base()
{}
}
Use the class like this
EmployeeCollection employeeCollection = new EmployeeCollection(list)
The other way around, it is not necessary to loop... you can make just .ToList()
ICollection<T> collection = new Collection<T>();
fill your collection using any method, and when you need the list, just do this:
List<T> list = collection.ToList();
after that you can use whatever you want with your list.
Have a good coding!
Does dot net have an interface like IEnumerable with a count property? I know about interfaces such as IList and ICollection which do offer a Count property but it seems like these interfaces were designed for mutable data structures first and use as a read only interface seems like an afterthought - the presence of an IsReadOnly field and mutators throwing exceptions when this property is true is IMO ample evidence for this.
For the time being I am using a custom interface called IReadOnlyCollection (see my own answer to this post) but I would be glad to know of other alternative approaches.
The key difference between the ICollection family and the IEnumerable family is the absence of certainty as to the count of items present (quite often the items will be generated/loaded/hydrated as needed) - in some cases, an Enumerable may not ever finish generating results, which is why the Count is missing.
Deriving and adding a Count is possible depending on your requirements, but it goes against this spirit, which is the purpose of ICollection - a collection of stuff that's all there.
Another way might be to use the System.Linq.Enumerable.Count method, i.e.
using System.Linq;
class X
{
void Y(IEnumerable<int> collection)
{
int itemCount = collection.Count();
}
}
or use the (System.Linq.Enumerable) .ToList() to pull all the items from the enumerator into a Collection and work from there.
(Also to answer your comment before having 50 rep:- the ".Count()" bit is a call to an extension method on the extension class System.Linq.Enumerable - the extension method is available on all things that derive from IEnumerable because the code has a "using System.Linq" which brings the extension methods in all classes in that namespace into scope - in this case its in the class Enumerable. If you're in VS, pressing F12 will bring you to the definition of S.L.Enumerable. BTW C# In Depth is a fantastic book for learning LINQ properly - its a page turner thats really helps you get the whole picture compared to learning the bits of LINQ piece by piece)
As of .Net 4.5, there are two new interfaces for this: IReadOnlyCollection<T> and IReadOnlyList<T>.
IReadOnlyCollection<T> is IEnumerable<T> with a Count property added, IReadOnlyList<T> also adds indexing.
It sounds like you really just want ReadOnlyCollection<T> - expose it as IList<T>, but by wrapping the original list like this you just get a read-only wrapper with an appropriate count.
Taking into consideration some of the comments I have decided to go with a wrapper class implementing a custom interface...
interface IReadOnlyCollection<T> : IEnumerable<T>
{
int Count { get; }
}
//This can now be not misused by downcasting to List
//The wrapper can also be used with lists since IList inherits from ICollection
public class CollectionWrapper<T> : IReadOnlyCollection<T>
{
public CollectionWrapper(ICollection<T> collection)
{
_collection = collection;
}
public int Count
{
get
{
return _collection.Count;
}
}
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)_collection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)((IEnumerable)_collection).GetEnumerator();
}
////////Private data///////
ICollection<T> _collection;
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
CollectionWrapper<int> collection = new CollectionWrapper<int>(list);
Console.WriteLine("Count:{0}", collection.Count);
foreach (var x in collection)
{
Console.WriteLine(x);
}
foreach (var x in (IEnumerable)collection)
{
Console.WriteLine(x);
}
}
}
Thanks all for your suggestions.
Edit: Now cannot be misused by downcasting to List (or whatever).
IList can return IsReadOnly as true, which marks the collection as readonly. Other than that I'm afraid I don't know of anything fitting.
Since it's an interface, you would have to implement the Count property yourself, why don't you create a new interface that inherits IEnumerator and add a Count property?
IList or ICollection would be the way to go, if you want to use the standard interfaces.
Note that you can "hide" methods required by the interface if you don't want them in your class's public interface -- for example, since it's meaningless to add things to a readonly collection you can do this:
void ICollection<DataType>.Add(DataType item)
{
throw new NotSupportedException();
}
public DataType this[int index]
{
get { return InnerList[index]; }
}
DataType IList<DataType>.this[int index]
{
get { return this[index]; }
set { throw new NotSupportedException(); }
}
etc.
An array can be cast to an IList, which makes the IList ReadOnly == true :)
You can get .Count on IEnumerable with an extension method if you add a reference to System.Linq (in 3.5 anyway).
As Jon Skeet mentions, you're much better off using System.Collections.ObjectModel.ReadOnlyCollection instead of creating your own wrapper class.
Then you can implement your sample as follows:
class Program {
static void Main(string[] args) {
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
ReadOnlyCollection<int> collection = new ReadOnlyCollection<int>(list);
Console.WriteLine("Count:{0}", collection.Count);
foreach (var x in collection) {
Console.WriteLine(x);
}
foreach (var x in (IEnumerable)collection) {
Console.WriteLine(x);
}
}
}