Converting IQueryable<T> object to another object?

You must hardcode the translation or you must first convert it to IEnumerable. IQueryable represents expression tree translated to some execution in used provider - in your case I believe it will be Entity framework. You can't use any automatic mapping in such query because it will be translated to SQL which will not understand your .net methods or AutoMapper. Projections to custom types are part of the query and must be hardcoded. You can create custom extension method to IQueryable and reuse it where you need:

public static IQueryabe<SimpleUser> ProjectToSimpleUser(this IQueryable<User> query)
{
    return query.Select(u => new SimpleUser
        {
            // Here map your fields
        });
}

Now you can use:

return repo.GetUsers().ProjectToSimpleUser();

In case of Entity framework SimpleUser mustn't be an mapped entity.


Provided that SimpleUser is assigneable to User (User is an interface of baseclass of SimpleUser), you can

 var users = simpleUsers.Cast<User>();

optionally with

 var users = simpleUsers.Cast<User>().AsQueryable();

And if you're not sure whether all items are actually Users, then you can use OfType<User> instead of Cast<User>


AutoMapper is the tool you want; it works via reflection and unless you tell it to do otherwise, it will map properties with the same name directly.

Auto-mapping is the technique.

Tags:

C#

.Net