如何在嵌套 AutoMapper Project().To<>() 后执行 LINQ 操作
How to perform LINQ operation after nested AutoMapper Project().To<>()
我在 C# LINQ 中嵌套了 Selects 并希望将它们转换为 AutoMapper 投影。
这是我最初查询的一部分:
.ThenByDescending(s => s.CreatedOn)
.Select(s => new SubmissionDetailsViewModel
{
// some mappings
Tests = s.TestRuns
.Select(tr => new TestRunViewModel
{
OrderBy = tr.Test.IsTrialTest ? TestCategoryType.Example : tr.Test.CategoryType,
// some other mappings
})
.GroupBy(tr => tr.OrderBy)
.OrderBy(tr => tr.Key)
})
如何配置 AutoMapper 进行投影,然后在投影的嵌套视图模型上调用 GroupBy。由于表达式中的可选参数,使用 Project().To() 而不是嵌套的 Select 无法编译。我尝试使用 AfterMap() 但它并没有起到作用。我应该执行以下操作吗:
.CreateMap<ICollection<TestRun>, IOrderedEnumerable<IGrouping<TestCategoryType, TestRunViewModel>>>()
.ProjectUsing()
然后复制嵌套的Select?但是这样我需要自己输入 select 而不是让 AutoMapper 为我投影它。有什么建议吗?
提前致谢!
我相信这是 AutoMapper 的 bug/shortcoming - 它无法处理嵌套表达式。 4.0 版应该可以解决这个问题。
我在 C# LINQ 中嵌套了 Selects 并希望将它们转换为 AutoMapper 投影。
这是我最初查询的一部分:
.ThenByDescending(s => s.CreatedOn)
.Select(s => new SubmissionDetailsViewModel
{
// some mappings
Tests = s.TestRuns
.Select(tr => new TestRunViewModel
{
OrderBy = tr.Test.IsTrialTest ? TestCategoryType.Example : tr.Test.CategoryType,
// some other mappings
})
.GroupBy(tr => tr.OrderBy)
.OrderBy(tr => tr.Key)
})
如何配置 AutoMapper 进行投影,然后在投影的嵌套视图模型上调用 GroupBy。由于表达式中的可选参数,使用 Project().To() 而不是嵌套的 Select 无法编译。我尝试使用 AfterMap() 但它并没有起到作用。我应该执行以下操作吗:
.CreateMap<ICollection<TestRun>, IOrderedEnumerable<IGrouping<TestCategoryType, TestRunViewModel>>>()
.ProjectUsing()
然后复制嵌套的Select?但是这样我需要自己输入 select 而不是让 AutoMapper 为我投影它。有什么建议吗?
提前致谢!
我相信这是 AutoMapper 的 bug/shortcoming - 它无法处理嵌套表达式。 4.0 版应该可以解决这个问题。