C# 使用AutoMapper为投影创建LINQ表达式
我将使用创建用于LINQ的表达式(例如LINQ到SQL):C# 使用AutoMapper为投影创建LINQ表达式,c#,linq,automapper,projection,C#,Linq,Automapper,Projection,我将使用创建用于LINQ的表达式(例如LINQ到SQL): void Main() { CreateMap(); Mapper.Engine.CreateMappExpression().ToString().Dump(); } 公共阶层人士 { 公共字符串名称{get;set;} 公共IEnumerable子项{get;set;} } 公共类个人 { 公共字符串名称{get;set;} 公共IEnumerable子项{get;set;} } 但是出现了一个映射“Children”属性的问题(
void Main()
{
CreateMap();
Mapper.Engine.CreateMappExpression().ToString().Dump();
}
公共阶层人士
{
公共字符串名称{get;set;}
公共IEnumerable子项{get;set;}
}
公共类个人
{
公共字符串名称{get;set;}
公共IEnumerable子项{get;set;}
}
但是出现了一个映射“Children”属性的问题(我在过去已经写过了)。作者告诉我们,问题是根类有一个自引用,但许多LINQ提供者不支持它 如果根类没有自引用,则AutoMapper 3的另一个问题是, 因为生成的LINQ表达式是:
x => new PersonDto() { Name = x.Name, Children = x.Children.Select(y => new AddressDto { Name = y.Name })}
而不是:
x => new PersonDto() { Name = x.Name, Children = IIF((x.Children == null), null, x.Children.Select(y => new AddressDto { Name = y.Name }))}
因此,如果根类的子属性为null,则投影将失败
目前无法将AutoMapper用于此目的。有什么问题?它有什么关系?@DmitryLedentsov不可能有这个表达式。@MatteoMigliore如果你不使用
IEnumerable
它的工作?是的,但它不是IEnumerable,而是IEnumerable(我修复了我发布的代码),t[]也有同样的问题。@MatteoMigliore恐怕除了AutoMapper
x => new PersonDto() { Name = x.Name, Children = IIF((x.Children == null), null, x.Children.Select(y => new AddressDto { Name = y.Name }))}