개체 목록을 가져 오는 중입니다. 이 개체에는 두 번째 클래스의 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에 아주 근본적인 무언가를 놓치고 있다고 생각한다.
내게는 하위 클래스가 엔티티 모델에없는 것 같습니다. 따라서 EF는 SQL을 유효한 Sql로 변환 할 수 없습니다. 두 가지 옵션이 표시됩니다. 모델에 하위 테이블을 추가하면 쿼리가 작동합니다. 또는 부모에게 .ToList()를 호출하면 Linq2Object로 쿼리 할 수 있습니다. – Dannydust
죄송합니다. 명확하지 않으면 맨 위에있는 상위 및 하위 클래스가 내 엔티티이므로 하위 항목이 상위에 IEnumerable로 있습니다. 내가 얻지 못하는 다른 것을 의미하지 않는다면? –
EF 코드를 사용합니까? 먼저 기존 데이터베이스에서 모델을 생성 했습니까? – Dannydust