我可以在LINQ to Entities的SELECT中執行子查詢來填充模型中的List嗎?

entity-framework-core linq

是否可以在LINQ to Entities查詢的.Select部分中有一個子查詢,以便在我的視圖模型中填充List<string> ?我在Select子句中找到了使用Linq To Entities子查詢來獲取字段值,但是當我嘗試這樣做時,我收到錯誤:

NotImplementedException:Microsoft.Data.Entity.Query.ResultOperators.Internal.QueryAnnotationResultOperator

這可能與Entity Framework 7和ASP.Net 5 MVC 6有關嗎?

var model = _DbContext.ReleaseVersions
                        .OrderByDescending(rv => rv.DateReleased)
                        .Select(rv => new ReleaseNotesVM()
                        {
                            ID = rv.ID,
                            CurrentVersion = string.Concat(rv.Major, '.', rv.Minor, '.', rv.Patch),
                            ReleaseNotes = _DbContext.ReleaseNotes
                                                    .Where(rn => rn.ReleaseVersionID == rv.ID)
                                                    .Include(rn => rn.ReleaseVersion)
                                                    .Select(rn => rn.Note)
                                                    .ToList()
                        }).FirstOrDefault();

ViewModel正在填充:

var model = _DbContext.ReleaseVersions
                        .OrderByDescending(rv => rv.DateReleased)
                        .Select(rv => new ReleaseNotesVM()
                        {
                            ID = rv.ID,
                            CurrentVersion = string.Concat(rv.Major, '.', rv.Minor, '.', rv.Patch),
                            ReleaseNotes = _DbContext.ReleaseNotes
                                                    .Where(rn => rn.ReleaseVersionID == rv.ID)
                                                    .Include(rn => rn.ReleaseVersion)
                                                    .Select(rn => rn.Note)
                                                    .ToList()
                        }).FirstOrDefault();

DataModel中:

var model = _DbContext.ReleaseVersions
                        .OrderByDescending(rv => rv.DateReleased)
                        .Select(rv => new ReleaseNotesVM()
                        {
                            ID = rv.ID,
                            CurrentVersion = string.Concat(rv.Major, '.', rv.Minor, '.', rv.Patch),
                            ReleaseNotes = _DbContext.ReleaseNotes
                                                    .Where(rn => rn.ReleaseVersionID == rv.ID)
                                                    .Include(rn => rn.ReleaseVersion)
                                                    .Select(rn => rn.Note)
                                                    .ToList()
                        }).FirstOrDefault();

一般承認的答案

如果我假設正確,你必須在ReleaseVersionsReleaseNotes之間有一對多的關係,這意味著ReleaseVersion類中應該有一個導航屬性引用ReleaseNotes的集合,使你可以像這樣重寫你的代碼:

var model = _DbContext.ReleaseVersions
                    .Include(rv => rv.ReleaseNotes)
                    .OrderByDescending(rv => rv.DateReleased)
                    .Select(rv => new ReleaseNotesVM
                    {
                        ID = rv.ID,
                        CurrentVersion = string.Concat(rv.Major, '.', rv.Minor, '.', rv.Patch),
                        ReleaseNotes = rv.ReleaseNotes.Select(rn => rn.Note).ToList()
                    }).FirstOrDefault();

更新

ReleaseNotesVM使用List<string> ReleaseNotes而不是ICollection<string>




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因