如何将Expression <func <dto,bool >>转换为Expression <func <entity,bool >>

时间:2019-03-31 17:08:54

标签: c# lambda expression

我想转换可以通过LINQ转换为SQL的真实表达式 从DTO表达式到实体表达式。 试图构建表达式,使用DTO实体映射器调用表达式,但表达式(逻辑上)未转换为SQL

// Function that contains the converter



public List<EngagementTypeModel> Filter(Expression<Func<EngagementTypeModel, bool>> filter) {
            var body = filter.Body;
            var Parameter = filter.Parameters[0];
            // Model is the DTO

            // NOT this ..
            // Expression<Func<EngagementTypeEntity,bool>> ConvertedExpression = (EngagementTypeEntity e) => filter.Compile()(BindEntityToModel(e));

            // Something like this
            var ConvertedExpression1 = Expression.Lambda<Func<EngagementTypeEntity, bool>>(filter, Expression.Parameter(typeof(EngagementTypeEntity), Parameter.Name));
            return _context.EngagementTypeEntities.Where(ConvertedExpression1).Select(x=>BindEntityToModel(x)).ToList();
        }


// API
[HttpPost]
        public ActionResult<string> SomeBusinessFilter()
        {
            _engagementTypeService.Filter(x => x.EngagementType == "foo" && x.Time < DateTime.Now && x.EngagementModel.EngaementTypeID == Guid.Empty);
            return Json(Ok("Hey"));
        }
``

0 个答案:

没有答案