This is a migrated thread and some comments may be shown as answers.

How convert IList<T> to List<T>

2 Answers 170 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrey
Top achievements
Rank 1
Andrey asked on 13 Jan 2011, 04:09 PM
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 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 13 Jan 2011, 04:47 PM
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.
0
Brian Womack
Top achievements
Rank 2
answered on 02 Feb 2011, 03:07 PM
Do pvMethods.ToList() instead of casting it.
Tags
Development (API, general questions)
Asked by
Andrey
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Brian Womack
Top achievements
Rank 2
Share this question
or