Search code examples
c#asp.net.netcastingilist

Converting anonymous type to IList


I'm getting an error I don't really understand from the following code:

public IList<Store> getNearbyStores(double x, double y)
{
    var result = (from T in
                      (
                        (from stores in dc.Stores
                           select new
                           {
                               stores.id,
                               stores.name,
                               stores.city,
                               stores.typeID,
                               stores.latitude,
                               stores.longitude,
                               stores.tag1,
                               stores.tag2,
                               Distance = (System.Double?)(6371 * Math.Acos((double)Math.Cos((double)(Math.PI * x) / 180) * Math.Cos((double)(Math.PI * stores.latitude) / 180) * Math.Cos((double)(Math.PI * stores.longitude) / 180 - (Math.PI * y) / 180) + Math.Sin((double)(Math.PI * x) / 180) * Math.Sin((double)(Math.PI * stores.latitude) / 180)))
                           }))
                  where
                    T.Distance < 5
                  orderby
                    T.Distance
                  select new
                  {
                      T.id,
                      T.name,
                      T.city,
                      T.typeID,
                      T.latitude,
                      T.longitude,
                      T.tag1,
                      T.tag2,
                      T.Distance
                  }).ToList();

    return result;
}

The error is:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IList<Store>'. An explicit conversion exists (are you missing a cast?) C:\Users\Joris\Desktop\ShopperNET\App_Code\DAL\DALstore.cs  104 16  C:\...\ShopperNET\

How do I convert an anonymous return type to the IList? I figured toList() would fix it, but it didn't.. I've tried some stuff I found around the web, like using just 'List', but none of it really helped me out.

Thanks in advance.


Solution

  • Your method signature is expecting a List<Store> to be returned, you could try returning that from your LINQ query, e.g.

    //top bit of query
    //.
    select new Store()
    {
        Id = T.id,
        Name = T.name,
        //etc..
    }).ToList();
    

    EDIT: Following on Pranay's suggestion below, the following should work:

    var result = from store in dc.Stores
                 let Distance = (System.Double?)(6371 * Math.Acos((double)Math.Cos((double)(Math.PI * x) / 180) * Math.Cos((double)(Math.PI * store.latitude) / 180) * Math.Cos((double)(Math.PI * store.longitude) / 180 - (Math.PI * y) / 180) + Math.Sin((double)(Math.PI * x) / 180) * Math.Sin((double)(Math.PI * store.latitude) / 180)))
                 where Distance < 5
                 order by Distance
                 select store;
    
    return result.ToList();