Imagine that we have three classes like this:
public class ParentType {
private ParentType() {}
public int Id { get; protected set; }
public SubType Sub { get; protected set; }
}
public class SubType{
private SubType(){}
public int Id { get; protected set; }
public ICollection<ColSubType> ColSubs{get; protected set;}
}
public class ColSubType{
private ColSubType(){}
public int Id { get; protected set; }
public SubType SubType { get; set; }
}
I have an anonymous Expression like this:
x => new
{
x.Id,
Sub = new
{
x.Sub.Id,
ColSubs = x.Sub.ColSubs.Select(u=> new {
u.Id
}).ToList()
}
}
I need to transform it to a non anonymous Expression like this:
x => new ParentType()
{
Id = x.Id,
Sub = new SubType()
{
Id = x.Sub.Id,
ColSubs = x.Sub.ColSubs.Select(u=> new ColSubs(){
Id = u.Id
}).ToList()
}
}
Thanks to @IvanStoev's answer for this question: Variable 'x.Sub' of type 'SubType' referenced from scope '' but it is not defined error I am able to transform the simple Expressions, but when I add x.Sub.ColSubs.Select(...)
I get the following error:
System.ArgumentException: Argument types do not match