Reuse QuerySpec projection

Posts   
 
    
scotru
User
Posts: 104
Joined: 16-Feb-2006
# Posted on: 10-Oct-2016 01:15:02   

I have a projection mapping lambda that I use with a dynamic QuerySpec query in LLBLGen

            var qf = new QueryFactory();
            var q = qf.Create()
                  .From(qf.Leave.LeftJoin(qf.Student).On(LeaveFields.StudentId == StudentFields.StudentId))
                  .Where(pred)
                  .OrderBy(LeaveFields.RequestDate.Descending())
                  .Select( () => new LeaveDTO
                  {
                      LeaveId = LeaveFields.LeaveId.ToValue<int>(),
                      RequestDate = LeaveFields.RequestDate.ToValue<DateTime>(),
                      LeaveDepart = LeaveFields.LeaveDepart.ToValue<DateTime>(),
                      LeaveReturn = LeaveFields.LeaveReturn.ToValue<DateTime>(),
                      ApprovalType = LeaveFields.ApprovalType.ToValue<string>(),
                      StudentId = StudentFields.StudentId.ToValue<int>(),
                      SchoolId = StudentFields.Schoolid.ToValue<string>(),
                      FirstName = StudentFields.FirstName.ToValue<string>(),
                      LastName = StudentFields.LastName.ToValue<string>(),
                  });

I would like to reuse the projection lambda in other queries called in other functions:

    () => new LeaveDTO
    {
        LeaveId = LeaveFields.LeaveId.ToValue<int>(),
        RequestDate = LeaveFields.RequestDate.ToValue<DateTime>(),
        LeaveDepart = LeaveFields.LeaveDepart.ToValue<DateTime>(),
        LeaveReturn = LeaveFields.LeaveReturn.ToValue<DateTime>(),
        ApprovalType = LeaveFields.ApprovalType.ToValue<string>(),
        StudentId = StudentFields.StudentId.ToValue<int>(),
        SchoolId = StudentFields.Schoolid.ToValue<string>(),
        FirstName = StudentFields.FirstName.ToValue<string>(),
        LastName = StudentFields.LastName.ToValue<string>(),
    }

How?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Oct-2016 10:22:22   

You could declare an expression and reuse it when needed:

System.Linq.Expressions.Expression<Func<LeaveDTO>> mySelector = (  () => new LeaveDTO
    {
        LeaveId = LeaveFields.LeaveId.ToValue<int>(),
        RequestDate = LeaveFields.RequestDate.ToValue<DateTime>(),
        LeaveDepart = LeaveFields.LeaveDepart.ToValue<DateTime>(),
        LeaveReturn = LeaveFields.LeaveReturn.ToValue<DateTime>(),
        ApprovalType = LeaveFields.ApprovalType.ToValue<string>(),
        StudentId = StudentFields.StudentId.ToValue<int>(),
        SchoolId = StudentFields.Schoolid.ToValue<string>(),
        FirstName = StudentFields.FirstName.ToValue<string>(),
        LastName = StudentFields.LastName.ToValue<string>(),
    };

// ....

var q = qf.Create()
    .From(qf.Leave.LeftJoin(qf.Student).On(LeaveFields.StudentId == StudentFields.StudentId))
    .Where(pred)
    .OrderBy(LeaveFields.RequestDate.Descending())
    .Select(mySelector);
David Elizondo | LLBLGen Support Team
scotru
User
Posts: 104
Joined: 16-Feb-2006
# Posted on: 16-Oct-2016 21:44:27   

Thanks! Exactly what I was looking for.

OSSistemes
User
Posts: 19
Joined: 20-Nov-2019
# Posted on: 28-Oct-2023 10:26:25   

Hi first, sorry for the revive the topic, but i found looking for a solution.

My question is possible to ADD some fields/attributes to "mySelector" after create it, this fields/attributes exist on the DTO.

My problem is I have a lot of queryspecs that share about 15 fields in this DTO, but have another 2 that only is used in one queryspecs

Thanks team!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 29-Oct-2023 08:35:01   

Please don't revive old topics

The expression is compiled by the C# compiler into a single expression, you can't augment it after that. The only way to change it is really to traverse the Expression<Func<T>> object and basically copy it and when doing so add the new MemberExpression objects, which is quite tedious to do.

I think you're better off with 2 methods, one creates the expression for the DTOs that share the same fields and one where you have the same fields but also add a couple.

Frans Bouma | Lead developer LLBLGen Pro