Automaper for collections of Expression

2019-08-21 19:00发布

问题:

I am using Automapper in my application and I faced an issue while mapping Expression>.

I have:

var predicateDto = _mapper.Map<Expression<Func<InventoryApplicationDto, bool>>, Expression<Func<tblInventoryApplication, bool>>>(predicate);
var applications = await _repository.SearchActivePagedAsync(predicateDto, page, pageSize);

This code works fine when I have only one value in predicate but it returns exception when I have List<T> as a predicate.

-- EDIT 1 Service method:

public async Task<PagedResult<InventoryApplicationDto>> SearchActivePagedAsync(
    Expression<Func<InventoryApplicationDto, bool>> predicate, int page, int pageSize)
{
    try
    {
        var predicateDto = _mapper.Map<Expression<Func<InventoryApplicationDto, bool>>, Expression<Func<tblInventoryApplication, bool>>>(predicate);
        var applications = await _repository.SearchActivePagedAsync(predicateDto, page, pageSize);

        var result = new PagedResult<InventoryApplicationDto>
        {
            CurrentPage = applications.CurrentPage,
            PageSize = applications.PageSize,
            RowCount = applications.RowCount,
            PageCount = applications.PageCount,
            Results = _mapper.Map<IEnumerable<tblInventoryApplication>, IEnumerable<InventoryApplicationDto>>(applications.Results)
        };

        return result;
    }
    catch (Exception ex)
    {
        var exc = ex.Message;
        throw;
    }
}

Controller method:

public async Task<ActionResult> Filter(
    List<InventoryRegionDto> regions,
    List<InventorySystemDto> systems,
    string phrase,
    int page = 0, int pageSize = 0)
{
    _applicationSearchParamter.Systems = systems;
    _applicationSearchParamter.Regions = regions;
    _applicationSearchParamter.Phrase = phrase;

    var applicationsSearchBuilder = new ApplicationSearchBuilder(_applicationSearchParamter);

    var currentPage = page == 0 ? _page : page;
    var currentPageSize = pageSize == 0 ? _pageSize : pageSize;
    var applications =
        await _applicationService.SearchActivePagedAsync(applicationsSearchBuilder.Build(), currentPage, currentPageSize);


    return PartialView("~/Views/Applications/_Applications.cshtml", applications);
}

Search builder method:

public Expression<Func<InventoryApplicationDto, bool>> Build()
        {
            var predicate = PredicateBuilder.New<InventoryApplicationDto>(true);

            if (!string.IsNullOrEmpty(_inventoryApplicationSearchParamter.Phrase))
                predicate.And(s => 
                    s.Name.Contains(_inventoryApplicationSearchParamter.Phrase) ||
                    s.Acronym.Contains(_inventoryApplicationSearchParamter.Phrase) ||
                    s.DetailedDescription.Contains(_inventoryApplicationSearchParamter.Phrase) ||
                    s.Comments.Contains(_inventoryApplicationSearchParamter.Phrase)
                    );

            if (_inventoryApplicationSearchParamter.Regions != null &&
                _inventoryApplicationSearchParamter.Regions.Any())
            {
                var predicateTemp = PredicateBuilder.New<InventoryApplicationDto>(true);

                foreach (var region in _inventoryApplicationSearchParamter.Regions)
                {
                    predicateTemp.Or(p => p.Region.Id == region.Id);
                }

                predicate.And(predicateTemp);
            }

            if (_inventoryApplicationSearchParamter.Systems != null &&
                _inventoryApplicationSearchParamter.Systems.Any())
            {
                var predicateTemp = PredicateBuilder.New<InventoryApplicationDto>(true);

                foreach (var system in _inventoryApplicationSearchParamter.Systems)
                {
                    predicateTemp.Or(s => s.Systems.Any(i => i.Id == system.Id));
                }

                predicate.And(predicateTemp);
            }

            return predicate;
        }

The Inner exception I am getting is :

ParameterExpression of type 'Infrastructure.Domain.tblInventoryApplication' cannot be used for delegate parameter of type 'Infrastructure.Dto.InventoryApplicationDto'

标签: c# AutoMapper