axelheer / nein-linq

NeinLinq provides helpful extensions for using LINQ providers such as Entity Framework that support only a minor subset of .NET functions, reusing functions, rewriting queries, even making them null-safe, and building dynamic queries using translatable predicates and selectors.
MIT License
521 stars 22 forks source link

[Help] Translating from LinqKit Invoke + Expand to NeinLinq #38

Closed SpaceOgre closed 10 months ago

SpaceOgre commented 2 years ago

Hi, please feel free to close this if you feel I should ask this somewhere else.

Today I'm using LinqKit to be able to reuse some Expression for selecting from collections, but I would like if I could only use NeinLinq since I want the Apply feature of it. I have tried for a long time now, but I can't figure out how to translate this to NeinLinq:

Base expression that should be reused:

    internal static class MapperForDTO
    {
        internal static Expression<Func<Subject, EducationEvent, PeriodData, MyStudentsListDTO>> ToDTO =>
            (subject, educationEvent, _) => new MyStudentsListDTO
            {
                Id = subject.Id,
                School Name = educationEvent.Name,
                //more stuff here
            };
    }

Code to use it for the specified entity:

Expression<Func<EducationEvent, MyStudentsListDTO>> mapper =
    educationEvent => MapperForDTO.ToDTO.Invoke(educationEvent.Subject, educationEvent, null);
var selectExpression = mapper.Expand();

Code for the second entity using the same base expression:

Expression<Func<PeriodData, MyStudentsListDTO>> mapper =
    periodData => MapperForDTO.ToDTO.Invoke(periodData.Subject, periodData.EducationEvent, periodData);
var selectExpression = mapper.Expand();

It might be that I'm just thinking about this the wrong way but any help would be appreciated.

axelheer commented 2 years ago

You could apply “Lambda Injection” to your MapperForDTO and use ToDTO directly within your queries.

If you need the “expanded” selectExpression outside your query, you could use the InjectLambdaVisitor somehow directly or play with the various Apply methods.

I can play with it, but I’m on the road right now.