AutoMapper Project (). For <T> and multiple lambda expressions

I'm having a problem getting a re-rendering lambda expression that works with AutoMapper Project (). To the extension method. I am using AutoMapper 3.1.1.

I want to be able to reuse the lambda expression by defining it as a variable, but I am having problems getting this to work with the extension method.

Please note that the code and lambda expression shown below are simplified, my lambda expression is very complicated and I want to reuse it in several places.

Here's the reuse of the lambda expression:

Func<Product, bool> myLambda = x => (x.Season.Id == 3);

Code block 1 uses Project (). For <> and reusable lambda, and not even compiled.

var dtos =
                _unitOfWork.ProductRepository.All()
                    .Where(myLambda)
                    .Project().To<ProductGridDTO>()
                    .OrderBy(dynamicSort)
                    .Skip(skip)
                    .Take(take)
                    .ToList();

It gives the following compilation error:

Error 18

'System.Collections.Generic.IEnumerable' "Project", "Project", "System.Collections.Generic.IEnumerable" ( ?)

2 .Select() Mapper.Map() , SQL- . Project(). < > SQL- , , .

var dtos =
                _unitOfWork.ProductRepository.All()
                    .Where(myLambda)
                    .Select(y => Mapper.Map(y, new ProductGridDTO()))
                    .OrderBy(dynamicSort)
                    .Skip(skip)
                    .Take(take)
                    .ToList();

3 Project(). To < > lambda, . , .

        var dtos3 =
            _unitOfWork.ProductRepository.All()
                .Where(x => (x.Season.Id == 3))
                .Project().To<ProductGridDTO>()
                .OrderBy(dynamicSort)
                .Skip(skip)
                .Take(take)
                .ToList();

Code Block 1. , -, ?

+4
1

IQueryable , Expressions. , -.

Expression<Func<Product, bool>> myLambda = x => (x.Season.Id == 3);

, .

, , .Where(, IEnumerable, .Project<T>() IQueyable<T>, .Where(, Expression<Func<TSource, bool>> Func<TSource, bool>.

+4

Source: https://habr.com/ru/post/1532221/


All Articles