views:

44

answers:

3

Hello,

I have a database repository with a bunch of access functions. I now want to build a fake repository which provides similar functionality for unittests. Unlike the real repository, this one uses simple lists instead of linqtosql generated model classes.

Most fake repository funtions look exactly like the real ones, just with an extra ToQueryable() at the end. However, I now have one that seems to require a more complicated cast.

   public class FakeUserRepository : IUserRepository {
          public IQueryable<SelectListItem> GetRecords(int userid) {
                    // fake_table is a list
                    return (from a in fake_table select
                    new SelectListItem {
                        Value = a.ID.ToString(),
                        Text = a.Name
                    });
          }
    }

Currently, this gives the error "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable.) I am not surprised by the error message but I have no idea how fix the cast.

Thanks for your help!

+1  A: 

There is an .AsQueryable() extension method on IEnumerable just for this, so add that to your returned query.

return (from ..).AsQueryable();
Hans Kesting
+1  A: 

you could do:

   public class FakeUserRepository : IUserRepository {
          public IQueryable<SelectListItem> GetRecords(int userid) {
                    // fake_table is a list
                    return (from a in fake_table select
                    new SelectListItem {
                        Value = a.ID.ToString(),
                        Text = a.Name
                    }).AsQueryable<SelectListItem>;
          }
    }
Koynov
+1  A: 

Just adding .AsQueryable() at the end of your linq query will make it work.

Matteo Mosca