【发布时间】:2021-08-17 20:38:02
【问题描述】:
找到了一种对抽象类的继承类使用投影的好方法 How to avoid repeating property projections when using EF Core inheritance?
var r1 = dbContext.Lessons.Select<Lesson, LessonDto>(l =>
l is ArticleLesson
? new ArticleLessonDto
{
Id = l.Id, // This is repeated below
Title = l.Title, // This is repeated below
// ...other properties that I would have to repeat below
Content = (l as ArticleLesson).Content,
}
: l is VideoLesson
? new VideoLessonDto
{
Id = l.Id, // This is repeated above
Title = l.Title, // This is repeated above
// ...other properties that I would have to repeat above
VideoUrl = (l as VideoLesson).VideoUrl,
}
: null
)
问题是它只在需要 dotnet 5 的 C# 9.0 中工作。
我正在从事的项目在 dotnet 4.7 和 C# 7.3 上运行,我不决定改变它。
与上面链接代码的主要区别是我需要一个 IQueryable 这意味着我不能使用 .ToList() 或任何可以实现查询的东西。
我尝试将条件提取为表达式和扩展方法,但 EF 尝试将其转换为 SQL,这当然会失败。
public static Expression<Func<Lesson, LessonDto>> CreateLessonDto = l => l.CreateLessonDto();
public static LessonDto CreateLessonDto(this Lesson Lesson)
{
if (Lesson is ArticleLesson)
return new ArticleLessonDto();
else if (Lesson is VideoLesson)
return new VideoLessonDto();
return null;
}
var r1 = dbContext.Lessons.Select(CreateLessonDto);
使用 .OfType 和 Union 似乎也不起作用。
任何解决此问题的想法都将受到高度赞赏。
【问题讨论】:
-
在第一个示例中,我没有看到 C# 9 特定的语法。也许问题是 EF 6 与 EF Core 5。你能确认你使用的 Entity Framework 的版本吗?
-
@vernou 标记并使用 EF6。这 ? :在选择中是 C# 9,因为条件返回不同的类型 web.archive.org/web/20210807210937/https://docs.microsoft.com/…
-
等等..你认为嵌套三元结构很好吗?
标签: c# entity-framework linq entity-framework-6