Using Automapper to return IQueryable
Asked Answered
J

2

12

I am trying to return an IQueryable object in one of my functions and using mapping (Automapper). It manage to return an IEnumerable object fine but as soon as i try to return an IQueryable object it throws me an error:

This is the error:

Missing type map configuration or unsupported mapping.

Mapping types: LLBLGenProQuery1 -> CostCentre SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery1[[Mail.DAL.EntityClasses.TblCostCentreEntity, Mail.DAL, Version=1.0.4638.16064, Culture=neutral, PublicKeyToken=null]] -> Mail.Model.CostCentre

Destination path: CostCentre

Source value: SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1[Mail.DAL.EntityClasses.TblCostCentreEntity]

This is the code:

Dim metaData As New LinqMetaData Dim q = From p In metaData.TblCostCentre _ Select p Mapper.CreateMap(Of TblCostCentreEntity, CostCentre)()

    Dim t As IEnumerable(Of CostCentre) = Mapper.Map(Of CostCentre)(q)
    'Select New CostCentre With {.Active = p.Active, .CostCentre = p.CostCentre, .CreatedBy = p.CreatedBy, .DateCreated = p.DateCreated, .DateLastModified = p.DateLastModified, .ModifiedBy = p.ModifiedBy, .CostCentreID = p.CostCentreId}

    Return t
Jewbaiting answered 13/9, 2012 at 1:39 Comment(1)
Please refer to this question for the proper solution.Drape
V
4

In order for Automapper to actually perform the mapping, it has to see each element in the IQueryable. Once you've iterated over a queryable, it is no longer queryable as it has been queried already.

Vituperation answered 13/9, 2012 at 1:45 Comment(2)
This link explains this further (only for llblgen users) - llblgen.com/tinyforum/Messages.aspx?ThreadID=17364Jewbaiting
Incorrect: Please see https://mcmap.net/q/523276/-does-automapper-support-linq for the solution.Agglutinative
F
7

For anyone that might miss the comment link, you can use Automapper's [QueryableExtensions][1], specifically ProjectTo. For example:

var collection = _db.Patients
     .ProjectTo<PatientDto>(_mapper.ConfigurationProvider);

This will create an IQueryable projection from the db entity.

Festivity answered 28/2, 2021 at 2:9 Comment(0)
V
4

In order for Automapper to actually perform the mapping, it has to see each element in the IQueryable. Once you've iterated over a queryable, it is no longer queryable as it has been queried already.

Vituperation answered 13/9, 2012 at 1:45 Comment(2)
This link explains this further (only for llblgen users) - llblgen.com/tinyforum/Messages.aspx?ThreadID=17364Jewbaiting
Incorrect: Please see https://mcmap.net/q/523276/-does-automapper-support-linq for the solution.Agglutinative

© 2022 - 2024 — McMap. All rights reserved.