How do I use Mapster to Project IQueryable

43 Views Asked by At

Good day! I am using Entity Framework 6 and .Net Framework 4.8, in which I have the following entity and DTO:

namespace Assist.Entity.Model
{
    public partial class procedure_detail
    {
        public procedure_detail()
        {
        }

        public int Procedure_Detail_RowID { get; set; }
        public int Procedure_RowID { get; set; }
        public int Modality_RowID { get; set; }
        
        public string Procedure_RefNo { get; set; }
        public int? Procedure_Priority_RowID { get; set; }
        public int Procedure_Status_RowID { get; set; }
        public int Bill_Status_RowID { get; set; }

        public bill_status bill_status { get; set; }
    }
}

namespace Assist.Service.Dto
{
    public partial class procedure_detail
    {
        public procedure_detail()
        {
        }

        public int Procedure_Detail_RowID { get; set; }
        public int Procedure_RowID { get; set; }
        public int Modality_RowID { get; set; }
        
        public string Procedure_RefNo { get; set; }
        public int? Procedure_Priority_RowID { get; set; }
        public int Procedure_Status_RowID { get; set; }
        public int Bill_Status_RowID { get; set; }

        public bill_status bill_status { get; set; }
    }
}

and I am using Mapster as follows:

TypeAdapterConfig<Dto.bill_status, Entity.Model.bill_status>.NewConfig().TwoWays().PreserveReference(true);
TypeAdapterConfig<Dto.procedure_detail, Entity.Model.procedure_detail>.NewConfig().TwoWays().PreserveReference(true);

I am getting the data from the database using Linq and returning an IQueryable, because there is a lot of data in the table. But I am getting an error when I try to project the IQueryable to the DTO object as follows:

var test00 = _repository.GetQueryable(w => !w.Procedure_Cancelled, ob => ob.OrderByDescending(o => o.procedure.Procedure_Date).ThenBy(o => o.procedure_priority.Procedure_Priority_Order).ThenBy(o => o.procedure.Procedure_RefNo), includeProperties: new string[] { "modality", "procedure.referral", "procedure.dependant_provider.dependant.title" });

var test01 = test00.ProjectToType<Dto.procedure_detail>(); //fails here

Error information is as follows:

   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTuple tuple, MapType mapType)
   at Mapster.TypeAdapterConfig.CreateProjectionCallExpression(TypeTuple tuple)
   at Mapster.TypeAdapterConfig.<>c__DisplayClass55_0`1.<AddToHash>b__0(TypeTuple types)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Mapster.TypeAdapterConfig.AddToHash[T](ConcurrentDictionary`2 hash, TypeTuple key, Func`2 func)
   at Mapster.TypeAdapterConfig.GetProjectionCallExpression(Type sourceType, Type destinationType)
   at Mapster.Extensions.ProjectToType[TDestination](IQueryable source, TypeAdapterConfig config)
   at Assist.Service.Implementation.ServiceProcedureDetail.LoadWorkList() in D:\Projects\Assist Products\Assist v2.0\Assist\Assist.Service\Implementation\ServiceProcedureDetail.cs:line 33

Inner exception is as follows:

Error while compiling source=Assist.Entity.Model.bill_status destination=Assist.Service.Dto.bill_status type=Projection

What am I doing wrong.

I am new to Mapster and mapping in general. So I was basically implemented from what I have seen online.

0

There are 0 best solutions below