0

I'm having a problem when use AutoMapper

my code:

public static IQueryable<IGrouping<Int64, GroupTimeSheetViewModel>> ToListGroupViewModel(this IQueryable<IGrouping<Int64, GroupTimeSheet>> entity)
    {
        return Mapper.Map<IQueryable<IGrouping<Int64, GroupTimeSheet>>, IQueryable<IGrouping<Int64, GroupTimeSheetViewModel>>>(entity);
    }

when I run my code, I get error:

The value "System.Collections.Generic.List1[PG.Admin.Models.TimeSheetHeaders.GroupTimeSheetViewModel]" is not of type "System.Linq.IGrouping2[System.Int64,PG.Admin.Models.TimeSheetHeaders.GroupTimeSheetViewModel]" and cannot be used in this generic collection. Parameter name: value

Khiem Nguyen
  • 403
  • 1
  • 8
  • 25

1 Answers1

2

AutoMapper can't map queryables directly. If I were you, I'd use AutoMapper's LINQ projection capabilities. It uses LINQ to build out the Select projection. That way, your resultant LINQ would be something like:

dbContext.GroupTimeSheets
   .GroupBy(ts => ts.Something)
   .Project().To<GroupTimeSheetViewModel>()
   .ToListAsync();
Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69