0

I'm having trouble understanding the nature of the exception generated when trying to execute this query against link to entities.

 var internalUsersFromRepos = userRepos.Users.Where(u => u.IsInternalUser)
                                                       .OrderBy(u => u.SirName)
                                                       .Skip((int)((page - 1) * usersPerPage))
                                                       .Take((int)usersPerPage)
                                                       .Select(u => new { User = u, Count = userRepos.Users.Count() })
                                                       .ToList();

Unable to cast the type 'System.Data.Objects.ObjectQuery1' to type 'System.Linq.IQueryable1'. LINQ to Entities only supports casting Entity Data Model primitive types.

If I replace "Count = userRepos.Users.Count()" with something constant like "Count = 3" then there is no exception so I believe this aspect of the query is key.

1 Answer 1

1

I think you can't combine ObjectQueries like this. Also your query is completly wrong. Use this:

var query = userRepos.Users.Where(u => u.IsInternalUser);
var count = query.Count();
var internalUsersFromRepos = query.OrderBy(u => u.SirName)
                                  .Skip((int)((page - 1) * usersPerPage))
                                  .Take((int)usersPerPage)
                                  .ToList();

I understand tha you want to return count and paged data in single round-trip but it doesn't look possible unless you use stored procedure. Data and count produces two different result sets so they can't be easily returned as single result set. Your current query tried to return list of users and for each returned user computed count of all users (without IsInternalUser filter)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.