Linq can't convert list to Observable collection

Answered Linq can't convert list to Observable collection

  • Monday, June 14, 2010 9:18 PM
     
     
     ObservableCollection<keyword> removeItems = this.keywordMatchItems.Where<keyword>(
                    param => param.IsSelected).ToList<keyword>();

    foreach (keyword lineItem in removeItems)
                {
                   this.keywordMatchItems.Remove(removeItems);
                }

    Cannot implicitly convert type 'System.Collections.Generic.List<keyword>' to 'System.Collections.ObjectModel.ObservableCollection<keyword>

All Replies

  • Tuesday, June 15, 2010 6:24 AM
     
     

    Hi,

    You cant cast a List<T> to an ObservableCollection<T>. You either need to use this ObservableCollection constructor overload or this extension method.

    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
  • Tuesday, June 15, 2010 1:58 PM
     
     
    Can you help me out with syntax.
  • Tuesday, June 15, 2010 2:17 PM
     
     Answered

    i just changed my syntax to this and it's working fine

    thanks a lot for your help

     

    LIist<keyword> removeItems = this.keywordMatchItems.Where<keyword>(
                    param => param.IsSelected).ToList<keyword>();

    foreach (keyword lineItem in removeItems)
                {
                   this.keywordMatchItems.Remove(lineItem);
                }

    Cannot implicitly convert type 'System.Collections.Generic.List<keyword>' to 'System.Collections.ObjectModel.ObservableCollection<keyword>

     

  • Tuesday, June 15, 2010 2:22 PM
     
     

    Either try:

     ObservableCollection<keyword> removeItems = new   ObservableCollection<keyword> ( this.keywordMatchItems.Where<keyword>(param => param.IsSelected).ToList<keyword>() );


    or if you included the extension method in the mentioned post, you can do:

     ObservableCollection<keyword> removeItems = this.keywordMatchItems.Where<keyword>(
                    param => param.IsSelected).ToList<keyword>().ToObservableCollection() ;

    Syed Mehroz Alam
    My Blog | My Articles
  • Thursday, August 19, 2010 7:49 PM
     
     

    Try This:

    List<string> _objLst = new List<string>();

     _objLst.Add("Example1");

     _objLst.Add("Example2");

    ObservableCollection<string> _objCollectionLst = new ObservableCollection<string>(_objLst);

     

    Applicable in .Net framework 3.0 and above.

  • Thursday, November 01, 2012 12:03 AM
     
     

    Hi,

    You cant cast a List<T> to an ObservableCollection<T>. You either need to use this ObservableCollection constructor overload or this extension method.

    Regards,


    Syed Mehroz Alam 

    You can use the ObservableCollection CTOR in the extension method, instead of the looping.

        public static class CollectionExtensions
        {
            public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
            {
                return enumerableList != null ? new ObservableCollection<T>(enumerableList) : null;
            }
        }


    • Edited by Wa Gwan Paul Thursday, November 01, 2012 12:04 AM
    •