0

개체 목록을 가져 오는 중입니다. 이 개체에는 두 번째 클래스의 IEnumerable 속성이 있습니다. 조건에 따라이 하위 목록을 필터링하려고합니다. 나는이 메서드를 호출 할 때IEnumerable 하위 속성 필터링

public IEnumerable<ParentViewModel> GetParents(int otherId) 
{ 
    var parents = _databaseContext.Parents 
     .Include(i => i.Children.Where(child => child.OtherId == otherId)); 

    return parents; 
} 

, 내가 얻을 : 여기

public class Parent 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public bool Active { get; set; } 
    public virtual IEnumerable<Child> Children { get; set; } 
} 

public class Child 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public int ParentId { get; set; } 
    public int OtherId { get; set; } 
    public bool Active { get; set; } 
    public virtual Parent Parent { get; set; } 
} 

내가 아이를 부모를 얻고 필터링을 시도하고있는 EF 코드 :

는 클래스가 있습니다 ArgumentException 메시지와 함께 :

The Include path expression must refer to a navigation property defined on the type. 
Use dotted paths for reference navigation properties and the Select operator for 
collection navigation properties. 

예외가 선택을 사용하여 언급 한 것을 감안할 때, 나는 tr 나에게 예외를 제공, 이것은 또한 불면

public IEnumerable<ParentViewModel> GetParents(int otherId) 
{ 
    var parents = _databaseContext.Parents 
     .Where(parent => parent.Active == true) 
     .Include(parent => parent.Children); 
     .Select(parent => new 
     { 
      Active = parent.Active, 
      Id = parent.Id, 
      Children = parent.Children 
       .Where(child => child.OtherId == propertyId) 
       .Select(child => new 
       { 
        Active = child.Active, 
        Id = child.Id, 
        ParentId = child.ParentId, 
        OtherId = child.OtherId, 
        Title = child.Title 
       }, 
      Title = parent.Title 
     }); 
    return parents; 
} 

을 :

The specified type member 'Children' is not supported in LINQ to 
Entities. Only initializers, entity members, and entity navigation 
properties are supported. 

을 그리고 아이디어에서 모든이야 곳이다 IED 너무 일을! 내가 뭘 잘못하고 있는지 모르겠지만 이것이 그렇게 힘들지 않았던 것처럼 느껴지지 않는다. 그래서 나는 Entity Framework에 아주 근본적인 무언가를 놓치고 있다고 생각한다.

+0

내게는 하위 클래스가 엔티티 모델에없는 것 같습니다. 따라서 EF는 SQL을 유효한 Sql로 변환 할 수 없습니다. 두 가지 옵션이 표시됩니다. 모델에 하위 테이블을 추가하면 쿼리가 작동합니다. 또는 부모에게 .ToList()를 호출하면 Linq2Object로 쿼리 할 수 ​​있습니다. – Dannydust

+0

죄송합니다. 명확하지 않으면 맨 위에있는 상위 및 하위 클래스가 내 엔티티이므로 하위 항목이 상위에 IEnumerable로 있습니다. 내가 얻지 못하는 다른 것을 의미하지 않는다면? –

+0

EF 코드를 사용합니까? 먼저 기존 데이터베이스에서 모델을 생성 했습니까? – Dannydust

답변

0

아, 그래! 나는 Code First에 익숙하지 않다. (나는 프로젝트에서 생성 된 모델을 사용하고있다.) 그러나 나는 EF가 이들 엔티티들이 관련되어 있다는 것을 인식하지 못한다고 확신한다. 오류 메시지 : "... 및 엔티티 탐색 속성이 지원됩니다"는 탐색 속성 (이 엔티티 간의 관계)을 정의해야한다고 알려줍니다.

public class Parent 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public bool Active { get; set; } 
    public virtual ICollection<Child> Children { get; set; } 
} 

public class Child 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public int ParentId { get; set; } 
    public int OtherId { get; set; } 
    public bool Active { get; set; } 
    public int ParentId [get;set;} 
    [ForeignKey("ParentId")] 
    public virtual Parent Parent { get; set; } 
} 
+0

컨텍스트를 수동으로 설정했습니다. 나는 그것을 ICollection으로 바꿨지만, 그것은 아무런 차이를 만들지 못했다. 나는 두렵다. –

+0

안녕하세요, 내 답변을 업데이트하고 데이터 주석과 속성을 추가하여 클래스를 변경했습니다. 이것이 작동하지 않는다면 데이터베이스 테이블 디자인을 게시 할 수 있습니까? – Dannydust

+0

작동하지 않았습니다. 나는 지금 당장이 문제를 해결하기 위해 시간을 다 써 버렸다. 그러나이 문제를 해결하기로 결심했다. –