Convert IEnumerable in List

ado.net asp.net c# entity-framework linq

Question

I am creating a control that receive in datasource a DataSet or List

How i convert a IEnumerable to List in a CreateChildControls event?

protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
    if (dataSource is System.Data.DataSet)
    {
    }
    else if(dataSource is IList)
    {
    }
}
1
7
7/25/2011 6:22:53 AM

Accepted Answer

Usually one would use the IEnumerable<T>.ToList() extensionmethod from Linq but in your case you can not use it (right away) because you have the non-generic IEnumerable interface. Sou you will have to cast it first (also using Linq):

datasource.Cast<object>().ToList();

No matter what you original collection actually ist, this will always succeed.

13
7/25/2011 6:30:58 AM

Popular Answer

I re-read your question and it seems that you are receiving a dataset which is ALREADY a list OR a DataSet, but it is cast to an IEnumerable. In this case, simply do

IList myList = (IList)datasource //will throw exception if invalid

or

IList myList = datasource as IList //mylist will be null if conversion cannot be made


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow