表达式<func>

时间:2017-09-22 17:43:13

标签: c# automapper

我在我的应用程序中使用Automapper,并且在映射Expression&gt;时遇到了问题。

我有:

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

当我在谓词中只有一个值但是当List<T>作为谓词时它返回异常时,此代码可以正常工作。

- 编辑1 服务方式:

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;
    }
}

控制器方法:

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);
}

搜索构建器方法:

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;
        }

我得到的内部异常是:

  

类型的ParameterExpression   &#39; Infrastructure.Domain.tblInventoryApplication&#39;不能使用   for delegate参数类型   &#39; Infrastructure.Dto.InventoryApplicationDto&#39;

0 个答案:

没有答案