I have a LINQ query to a database, with multiple linked tables, and need to return (SELECT) different fields depending on inputs.
ClassA has ParamA, ParamB, and
This may be a bit verbose, but Expressions can get that way, for example:
var typeClassA = typeof(ClassA);
var typeClassB = typeof(ClassB);
var parameterExpressionP = Expression.Parameter(typeClassB, "p");
var newExpression = Expression.New(typeClassB);
var paramDPropertyExpression = Expression.Property(parameterExpressionP, "ParamD");
var paramDMemberBinding = Expression.Bind(typeClassB.GetProperty("ParamD"), paramDPropertyExpression);
var paramEPropertyExpression = Expression.Property(parameterExpressionP, "ParamE");
var paramEMemberBinding = Expression.Bind(typeClassB.GetProperty("ParamE"), paramEPropertyExpression);
var memberInitExpression = Expression.MemberInit(
newExpression,
paramDMemberBinding, paramEMemberBinding);
var projectionExpression = Expression.Lambda<Func<ClassB, ClassB>>(memberInitExpression, parameterExpressionP);
var parameterExpressionC = Expression.Parameter(typeClassA, "c");
var selectParamExpression = Expression.Property(parameterExpressionC, "ClassBs");
var selectExpression = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.Select),
new[] { typeClassB, typeClassB },
selectParamExpression, projectionExpression);
var toListExpression = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.ToList),
new[] { typeClassB },
selectExpression);
This will create an expression something like:
c.ClassBs.Select(p => new ClassB() {ParamD = p.ParamD, ParamE = p.ParamE}).ToList()