views:

451

answers:

6

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

+4  A: 

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?

Jon Skeet
perhaps the right number of items was 0
Jimmy
+9  A: 

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(); 
}
Keith
You shouldn't need the `Cast<IRegion>()` now that you're projecting into a concrete type.
cottsak
@cottsak: Yes he will - `IList<T>` is invariant.
Jon Skeet
I have my Linq autogenerated class Region that implements IRegion.When I use it, I got another error messageExplicit construction of entity type 'xxxx.LinqToSql.xxxx.Region' in query is not allowed.
nachid
@Skeet: Yes of course. `IRegion` is the return type. my bad
cottsak
@nachid - ahh, I think we have a fix then! `Database.RegionDataSource` returns `Region`. To fix all you need is to end with `select c`
Keith
You're right KeithThings are getting better now
nachid
+2  A: 

The cast to IRegion won't work. You're selecting an anonymous type that won't implement IRegion. Is there a way you can create an instance of something that implements IRegion?

Mike Two
+1  A: 

Maybe you need something like this:

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

     return query.ToList(); 
}
cottsak
I need somehow to cast to IRegion before I return my queryThe compiler complains that it cannot convert IList<Region> to IList<IRegion>
nachid
Skeet is correct. You need the `Cast<IRegion>()`
cottsak
A: 

1 Maybe you need something like this:

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

 return query.ToList();  

}

stackuser1
I ended up doing this public IList<IRegion> GetRegionList(string countryCode) { var query = from c in Database.RegionDataSource where (c.CountryCode == countryCode) orderby c.Name select c; return query.Cast<IRegion>().ToList(); } I'll return here soon and let you know Thank you for your valuable help
nachid
A: 

Sorry everybody and thank you so much

I am still very new this site and quickly found myself confused

However, you helped me understood what was happening and why It wasn't working the way I expected

Thank you again

nachid