Reputation: 11722
I'm fetching the data with OData syntax: http://server.com/api/users?$skip=20&$top=10
I'm trying to add index to every fetched entity in my Web API Controller. Every time I'm getting the following error:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable
1[WebPortal.Models.UserDto] Select[UserInDatabase,UserDto](System.Linq.IQueryable
1[DataContext.UserInDatabase], System.Linq.Expressions.Expression1[System.Func
3[DataContext.Media,System.Int32,WebPortal.Models.UserDto]])' method, and this method cannot be translated into a store expression.","ExceptionType":"System.NotSupportedException"
As I understand I couldn't use Select(u, index)=> selector while working with Entity Framework context (works fine for in memory collection). Unfortunately I'm using it to expose my collection via OData + QueryableAttribute:
public class UsersController : ApiController
{
[Queryable]
public IQueryable<UserDto> Get()
{
return _repository.Users
.Select((u, i) => new UserDto
{
Index = i,
Name = u.Name,
Age = u.Age
})
.AsQueryable();
}
}
How can I modify fetch process to continue to use OData syntax and have ability to return entities with index to requesting side?
my entities:
public class UserInDatabase
{
public string Name { get; set; }
public int Age { get; set; }
}
public class UserDto
{
public int Index { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
TIA
Upvotes: 0
Views: 700
Reputation: 28046
Try changing this line:
return _repository.Users
to
return _repository.Users.AsEnumerable()
This should force the query to execute before projecting onto your DTO.
Upvotes: 1