I'm attempting to create a small "automapper-esq" utility that will take a LinqToSql entity and map it to a "projection class".
So far I have something like this:
class Entity
{
public int ID { get; set; }
public string WantedProperty { get; set; }
public string UnWantedPropertyData { get; set; }
...More Unwanted Properties...
public IEnumerable<ChildEntity> ChildEntities { get; set; }
}
class EntityProjection
{
public int ID { get; set; }
public string WantedProperty { get; set; }
public IEnumerable<ChildEntityProjection> ChildEntities { get; set; }
}
class ChildEntityProjection
{
public int ID { get; set; }
public string WantedProperty { get; set; }
public string UnWantedPropertyData { get; set; }
...More Unwanted Properties...
}
var results = context.Table.Select(ProjectionHelper.BuildProjection<Entity,EntityProjection>());
where BuildProjection returns:
Expression<Func<TSource, TResult>>
which essentially creates a lambda like this:
A => new EntityProjection() { ID = A.ID, WantedProperty = A.WantedProperty }
Now the tricky part...I'd like to be able to project association properties of the "parent" entity as well. Essentially what I need is to get something like this:
A => new EntityProjection() {
ID = A.ID,
WantedProperty = A.WantedProperty,
ChildEntities = A.ChildEntities.Select(B => new ChildEntityProjection {
ID = B.ID,
WantedProperty = B.WantedProperty
}
}
I have gotten as far as getting this part:
A => new EntityProjection() {
ID = A.ID,
WantedProperty = A.WantedProperty,
ChildEntities = System.Collections.Generic.List1[ChildEntity].Select(B => new ChildEntityProjection {
ID = B.ID,
WantedProperty = B.WantedProperty
}
}
By doing this:
IQueryable<ChildEntity> list = new List<ChildEtity>().AsQueryable();
Expression _selectExpression = Expression.Call(
typeof(Queryable),
"Select",
new Type[] { typeof(ChildEntity), typeof(ChildEntityProjection) },
Expression.Constant(list),
_nestedLambda);
Here is where I am stuck at the moment...I am getting a little confused when attempting to replace Expression.Constant(list) with some other expression that represents the actual datatype for the property so that "System.Collections.Generic.List1[ChildEntity].Select(B=>..." will be replaced with "A.ChildEntities.Select(B=>..."
Any ideas?