Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<> to IList<>

I am using Linq to query my database and returning a generic IList.

Whatever I tried I couldn't convert an IQueryable to an IList.

Here is my code.

I cannot write simpler than this and I don't understand why it is not working.

public  IList<IRegion> GetRegionList(string countryCode)
{
    var query = from c in Database.RegionDataSource
                where (c.CountryCode == countryCode)
                orderby c.Name
               select new {c.RegionCode, c.RegionName}; 

     return query.Cast<IRegion>().ToList(); 
}

This returns an list with the right number of items but they are all empty Please help, I am bloqued with this for a couple of days now

like image 380
nachid Avatar asked Apr 28 '10 09:04

nachid


People also ask

How do I convert IEnumerable to IList?

The interface IEnumerable<T> is not convertible to IList and attempting to pass a variable of type IEnumerable<T> to a method taking IList will result in a compilation error. Convert the IEnumerable<T> instance to a new object which is convertible to IList . For example, in 3.5+ you can call the .

What is the difference between IList and IEnumerable?

Like IEnumerable, IList is also best to query data from in-memory collections like List, Array etc. IList is useful when you want to Add or remove items from the list. IList can find out the no of elements in the collection without iterating the collection. IList supports deferred execution.

Should I return IEnumerable or Icollection?

If you do not counting in your external code it is always better to return IEnumerable, because later you can change your implementation (without external code impact), for example, for yield iterator logic and conserve memory resources (very good language feature by the way).

Is IEnumerable faster than List?

IEnumerable is conceptually faster than List because of the deferred execution. Deferred execution makes IEnumerable faster because it only gets the data when needed. Contrary to Lists having the data in-memory all the time.


2 Answers

Your select statement returns an anonymous type: new {c.RegionCode, c.RegionName}

This can't be converted to IRegion - that would basically be Duck-typing, which C# doesn't support.

Your linq statement should return a type that implements IRegion - then your code should work.

However it shouldn't run - the Cast<IRegion> should throw a runtime exception.

Basically:

// this isn't anonymous, and should cast
public class MyRegion : IRegion {
    public string RegionCode {get;set;}
    public string RegionName {get;set;}
}

public  IList<IRegion> GetRegionList(string countryCode)
{
    var query = from c in Database.RegionDataSource
                where (c.CountryCode == countryCode)
                orderby c.Name
               select new MyRegion {RegionCode = c.RegionCode, RegionName = c.RegionName}; 

     return query.Cast<IRegion>().ToList(); 
}

Update

If the underlying Linq type implements IRegion this can be a lot simpler:

public  IList<IRegion> GetRegionList(string countryCode)
{
    var query = 
        from region in Database.RegionDataSource
        where region.CountryCode == countryCode
        orderby region.Name
        select region; 

     return query.ToList(); 
}
like image 149
Keith Avatar answered Oct 23 '22 18:10

Keith


I'm surprised it's not just failing completely - you're trying to cast each result to an IRegion, but you're generating instances of an anonymous type, which certainly won't implement IRegion.

Do you have a concrete type which implements IRegion?

like image 38
Jon Skeet Avatar answered Oct 23 '22 18:10

Jon Skeet