Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Automapper to return IQueryable

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
like image 879
Baahubali Avatar asked Sep 13 '12 01:09

Baahubali


2 Answers

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.

like image 103
armen.shimoon Avatar answered Sep 27 '22 00:09

armen.shimoon


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.

like image 43
Paul DeVito Avatar answered Sep 26 '22 00:09

Paul DeVito