How convert IList<T> to List<T>

3 posts, 0 answers
  1. Andrey
    Andrey avatar
    21 posts
    Member since:
    Aug 2010

    Posted 13 Jan 2011 Link to this post

    Hello Telerik team!
    private IList<PersonPredictedValueMethod> pvMethods { get; set; }
     
    [Transient]        
    public List<PersonPredictedValueMethod> PVMethods
    {
        get
        {
            if (pvMethods != null)
            {                    
                return (List<PersonPredictedValueMethod>)pvMethods;
            }
            return null;
        }
        set { pvMethods = value; }
    }

    In such case I have an exception

    Unable to cast object of type 'Telerik.OpenAccess.TrackedList`1[PersonPredictedValueMethod]' to type 'System.Collections.Generic.List`1[PersonPredictedValueMethod]'

  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 13 Jan 2011 Link to this post

    Hello Andrey,

    there is no cast possible, as not all IList<T> implementations are List<T>.  You must either expose the IList<T> interface in your classes (which will allow our change tracking to work then) or you have to copy the IList<T> into a new List<T>(). But this will effectively disable the change tracking, as we cannot detect modifications to the list.

    Greetings,
    Thomas
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
  4. Brian Womack
    Brian Womack avatar
    4 posts
    Member since:
    Nov 2008

    Posted 02 Feb 2011 Link to this post

    Do pvMethods.ToList() instead of casting it.
Back to Top