2

I need to map IQueryable<User> to IQueryable<SimpleUser> with ValueInjecter.

Is this possible?

I tried:

return userRepo.GetUsers()
               .Select(o => new SimpleUser().InjectFrom(o))
               .Cast<SimpleUser>();

But this cannot be translated to a stored expression...well, the method InjectFrom.

Can automapper do this?

I want something similar to this:

return from i in userRepo.GetUsers()
      select new SimpleUser{
            i.UserId,
            i.Name
      };

but with using some kind of mapper tool.

Omu
  • 69,856
  • 92
  • 277
  • 407
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

1 Answers1

3

Convert the collection to objects before doing the select and it should work. Updated using PredicateBuilder to show filtering and paging and Dynamic LINQ for sorting.

var predicate = new PredicateBuilder<User>.True();
if (!string.IsNullOrEmpty( typeFilter ))
{
    predicate = predicate.And( u => u.Type == typeFilter );
}
if (!string.IsNullOrEmpty( nameFilter ))
{
    predicate = predicate.And( u => u.Name.StartsWith( nameFilter ));
}

// assumes sortColumn matches one of your user properties and
// sortDirection is either "ASC" or "DESC"
string sortOrder = string.Format( "{0} {1}", sortColumn, sortDirection ); 

return userRepo.GetUsers()
               .Where( predicate )
               .OrderBy( sortOrder )
               .Skip( (page-1) * usersPerPage )
               .Take( usersPerPage )
               .ToList()  // force the query and make these objects
               .Select(o => new SimpleUser().InjectFrom(o))
               .Cast<SimpleUser>();
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Is there a way to do this before evaluating the query? I have a grid control that utilizes the IQueryable to do filtering, paging and sorting. – Shawn Mclean Mar 26 '11 at 17:03
  • @Lol coder - you need to do the filtering, sorting, paging before you attempt to do the conversion. Plain and simple, the conversion won't translate to SQL so you have to do it using LINQ to objects which means all the objects have to be in memory. Would it be possible to map a view corresponding to SimpleUser? You could then set up SQL function corresponding to the view that allows you to pass in parameters do to the filtering, sorting, paging. – tvanfosson Mar 26 '11 at 17:07
  • @tvanfosson I edited my question to show what I want to achieve. – Shawn Mclean Mar 26 '11 at 19:45
  • @Lol coder - you're retrieving all of the entries in the DB with your update. you haven't shown anything that would prevent you from materializing the query (using ToList) then applying your mapper code. I suspect that it's also possible to do the filtering, etc. before conversion. I'll update with a sample. – tvanfosson Mar 26 '11 at 20:43
  • Should I just create in the repository, parameters to pass up the filters, sorting and other paging stuff? – Shawn Mclean Mar 26 '11 at 21:16
  • @Lol coder - if you're going to reuse that sort of behavior in other places, yes. – tvanfosson Mar 26 '11 at 23:34