0

私は以前、コース内から関連するサブセクションのリストを正常に返す、私の AdminController 内からの次のコード行を持っていました:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{ 
  var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
  {            
    sectionID = x.CourseSectionID,
    sectionTitle = x.Title
  );
  return Json(Sections, JsonRequestBehavior.AllowGet);
}

dbcontext を呼び出すのは悪い習慣であるため、コントローラーからこれを取り出すように通知されたので、これを AdminViewModel に移動しました。私の AdminViewModel 内には、変数public List CourseSectionList { get; があります。設定; この変数に JSON リクエストの詳細を設定しようとしています私のコードは次のとおりです。

AdminViewModel

public void GetCourseSectionDetails(int courseID)
{
  var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
  {
    CourseSectionID = x.CourseSectionID,
    Title = x.Title
  });
  this.CourseSectionList = Sections.ToList();
}

管理者コントローラ

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{ 
  avm.GetCourseSectionDetails(courseID);
  var Sections = avm.CourseSectionList.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
  {            
    sectionID = x.CourseSectionID,
    sectionTitle = x.Title
  });
  System.Diagnostics.EventLog.WriteEntry("Application", "JSON=" + Sections.ToList(), System.Diagnostics.EventLogEntryType.Error);
  return Json(Sections, JsonRequestBehavior.AllowGet);
}

The entity or complex type 'MetaLearning.Data.CourseSection' cannot be construct in a LINQ to Entities query.というエラーが表示されます。セクションを使用して this.CourseSectionList 変数を設定するにはどうすればよいですか?

4

3 に答える 3

2

エラー メッセージで指摘されているようにlinq to entities、では、

.Select(m => new <Entity>{bla bla})

ここで<Entity>... はモデルのエンティティの 1 つです。

したがって、必要なプロパティを持つ「非モデル」クラス (DTO) を使用するか、選択する前に列挙する必要があります (linq to objectsその制限がないため) 。

.ToList()
.Select(m => new <Entity>{bla bla});

ここでそれが不可能な理由のいくつかの素晴らしい説明を見つけることができます

編集 :

エンティティの一部のプロパティのみを取得し、DTO を使用したくない場合は、次のようなこともできます。

return ctx
        .CourseSection
         .Where(cs => cs.CourseID.Equals(courseID))
         //use an anonymous object to retrieve only the wanted properties
         .Select(x => new 
                {
                    c= x.CourseSectionID,
                    t= x.Title,
                })
         //enumerate, good bye linq2entities
         .ToList()
         //welcome to linq2objects
         .Select(m => new CourseSection {
                     CourseSectionID = m.c,
                     Title = m.t,
          })
          .ToList();
于 2013-08-21T16:56:00.340 に答える
0

Darinの回答をガイドとして使用して、次のようにこれを行いました。

ビューモデル

public void GetCourseSectionDetails(int courseID)
    {
        this.CourseSectionList = dbcontext.CourseSection.AsEnumerable().Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
        {
            CourseSectionID = x.CourseSectionID,
            Title = x.Title
        }).ToList();
    }

コントローラ

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetCourseSections(int courseID)
    {             
        var sections = avm.CourseSectionList;
        return Json(sections, JsonRequestBehavior.AllowGet);
    }
于 2013-08-22T08:35:04.967 に答える