Include from IEnumerable to Queryable in select statement?

I have a database repository with a bunch of access functions. Now I want to create a fake repository that provides similar functionality for unittests. Unlike a real repository, this one uses simple lists, not classes created using linqtosql.

Most fake repository functions look exactly the same as real ones, with just the extra ToQueryable () at the end. However, I now have one that seems to require a more complex translation.

   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
                    });
          }
    }

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

+3
3

:

   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>;
          }
    }
+8

IEnumerable .AsQueryable(), .

return (from ..).AsQueryable();
+5

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

+3
source

Source: https://habr.com/ru/post/1751164/


All Articles