locked
Linq can't convert list to Observable collection RRS feed

  • Question

  •  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>
    Monday, June 14, 2010 9:18 PM

Answers

  • 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:17 PM

All replies

  • 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 6:24 AM
  • Can you help me out with syntax.
    Tuesday, June 15, 2010 1:58 PM
  • 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:17 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
    Tuesday, June 15, 2010 2:22 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, August 19, 2010 7:49 PM
  • 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 1, 2012 12:04 AM
    Thursday, November 1, 2012 12:03 AM