C# 如何合并对象表达式?

C# 如何合并对象表达式?,c#,linq,lambda,C#,Linq,Lambda,我的问题是如何合并像这样的对象表达式 Func<T, object> “((p=>p.Info和p=>p.Profile))”这到底是什么意思?p.Info或p.Profile都不是布尔值,那么和在这里代表什么?是的,Info或Profile都不是布尔值。它们是我正在更新的示例中尝试使用的对象。它连接到实体框架并加载相关数据。由于这一切都是通过RazorPage->ServiceLayer->Repository完成的,而且我已经准备好了通用的东西,所以我需要做这个映射。这一切只适

我的问题是如何合并像这样的对象表达式

Func<T, object>

“((p=>p.Info和p=>p.Profile))”
这到底是什么意思?
p.Info
p.Profile
都不是布尔值,那么
在这里代表什么?是的,Info或Profile都不是布尔值。它们是我正在更新的示例中尝试使用的对象。它连接到实体框架并加载相关数据。由于这一切都是通过RazorPage->ServiceLayer->Repository完成的,而且我已经准备好了通用的东西,所以我需要做这个映射。这一切只适用于一种表达,而不是更多。您可以在github代码中看到“和”]让我们后退一步<代码>我正在尝试将所有参数合并成一个Automapper可以处理的表达式您是否尝试过手工编写一个适合您的用例并可以由Automapper处理的表达式(没有任何奇特的自动合并内容)?如果是的话,你能给我们看看吗?如果某个东西不存在,那么尝试动态构建它是没有意义的,因为ProjectTo不需要任何包含。它会获取DTO中的所有内容,但您可以通过显式扩展排除这些内容。Kevin,这一切都很好。不过,我更新了我的示例,使用了两个测试,真正显示了我在映射实现位置方面的问题。自动映射错误就在那里。我也不走运地试图找到一个解决方案,所以我去寻找一个表达式组合
“((p=>p.Info和p=>p.Profile))”
它到底是什么意思?
p.Info
p.Profile
都不是布尔值,那么
在这里代表什么?是的,Info或Profile都不是布尔值。它们是我正在更新的示例中尝试使用的对象。它连接到实体框架并加载相关数据。由于这一切都是通过RazorPage->ServiceLayer->Repository完成的,而且我已经准备好了通用的东西,所以我需要做这个映射。这一切只适用于一种表达,而不是更多。您可以在github代码中看到“和”]让我们后退一步<代码>我正在尝试将所有参数合并成一个Automapper可以处理的表达式您是否尝试过手工编写一个适合您的用例并可以由Automapper处理的表达式(没有任何奇特的自动合并内容)?如果是的话,你能给我们看看吗?如果某个东西不存在,那么尝试动态构建它是没有意义的,因为ProjectTo不需要任何包含。它会获取DTO中的所有内容,但您可以通过显式扩展排除这些内容。Kevin,这一切都很好。不过,我更新了我的示例,使用了两个测试,真正显示了我在映射实现位置方面的问题。自动映射错误就在那里。我试图找到一个解决方案,但也没有运气,所以我去寻找一个表达式组合
Expression<Func<Employee, bool>> filter1 = p => p.Name == "John Doe";
Expression<Func<Employee, bool>> filter2 = p => p.Address == "Address 123";

// Two boolean expressions combined
Expression<Func<Employee, bool>> filterCombined = filter1.And(filter2);

// Works as expected
filterCombined.Body.ToString().ShouldBe("((p.Name == \"John Doe\") And (p.Address == \"Address 123\"))");
Expression<Func<Employee, object>> filter1 = p => p.Info;
Expression<Func<Employee, object>> filter2 = p => p.Profile;

// Trying to combine two object expressions fails
Expression<Func<Employee, object>> filterCombined = ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate(filter1, filter2);

filterCombined.Body.ToString().ShouldBe("((p => p.Info And p => p.Profile))"); //Something like this anyway...
public MyModelDTO GetById(int? id, params Expression<Func<MyModelDTO , object>>[] includeExpressions)
{
  // here Automapper (with Automapper.Extension.ExpressionMapping) 
  // fails to do the mapping so I´m trying to combine them into one
  Expression<Func<MyModel, object>> mappedExpression= MapToType<Expression<Func<MyModel, object>>>(includeExpressions);

   // call the repository with EF expression
   var myModel = MyRepoGetById(id,mappedExpression);
   ...map myModel back to dto
   return myMappedDtoModel;
}

// The repository method
public MyModel  MyRepoGetById(int id, params Expression<Func<MyModel, object>>[] includeExpressions)
{
        if (includeExpressions.Any())
        {
            IQueryable<T> set = includeExpressions
              .Aggregate<Expression<Func<T, object>>, IQueryable<T>>
                (dbContext.Set<T>(), (current, expression) => current.Include(expression));

            return await set.SingleOrDefaultAsync(s => s.Id == id);
        }

        return dbContext.Set<T>().Find(id);
}
var result  = await service.GetById(id,x => x.ClassProperty, x => x.AnotherClassProperty);