Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 筛选IEnumerable子属性_C#_Entity Framework_Ef Code First_Entity Framework 5_Automapper - Fatal编程技术网

C# 筛选IEnumerable子属性

C# 筛选IEnumerable子属性,c#,entity-framework,ef-code-first,entity-framework-5,automapper,C#,Entity Framework,Ef Code First,Entity Framework 5,Automapper,我正试图带回一个对象列表。此对象具有第二类的IEnumerable属性。我正在尝试根据条件筛选此子列表 有以下几类: public class Parent { public int Id { get; set; } public string Title { get; set; } public bool Active { get; set; } public virtual IEnumerable<Child> Children { get; se

我正试图带回一个对象列表。此对象具有第二类的IEnumerable属性。我正在尝试根据条件筛选此子列表

有以下几类:

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; }
}
考虑到异常提到使用Select,我也尝试过这样做:

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;
}

这就是我没有创意的地方!我不知道我做错了什么,但这并不像以前那么难,所以我猜我错过了实体框架的一些非常基本的东西。

啊,好吧!我首先不熟悉代码(我在我的项目中使用生成的模型),但我非常确定EF没有认识到这些实体是相关的。错误消息“…和实体导航属性受支持”告诉我应该定义导航属性(这些实体之间的关系)

公共类父类
{
[关键]
公共int Id{get;set;}
公共字符串标题{get;set;}
公共bool活动{get;set;}
公共虚拟ICollection子项{get;set;}
}
公营儿童
{
[关键]
公共int Id{get;set;}
公共字符串标题{get;set;}
public int ParentId{get;set;}
public int OtherId{get;set;}
公共bool活动{get;set;}
public int ParentId[get;set;}
[ForeignKey(“ParentId”)]
公共虚拟父级{get;set;}
}

对我来说,您的子类似乎不在实体模型中。因此EF无法将其转换为有效的Sql。因此,我看到两个选项:将子表添加到模型中,您的查询应该可以工作或调用.ToList()在父对象上,然后您可以使用LINQ2对象进行查询。很抱歉,如果我不清楚,顶部的父类和子类是我的实体,因此子类作为IEnumerable在父对象上。除非您指的是我没有得到的其他内容?您是否先使用EF代码,或者您是否从现有数据库生成模型?我先使用代码进行集成使用数据库进行ract,但它是一个现有的数据库,而不是首先使用代码创建的数据库。这可能只是一个输入错误,但在代码片段中,您没有创建新的ParentViewModels。您能找出两个parent.Children中的哪一个是罪魁祸首吗?我确实手动设置了上下文。我已将其更改为ICollection,但这恐怕没有什么区别。嗨,我更新了我的答案,并通过添加数据注释和属性更改了你的类。如果这不起作用,你能发布数据库表的设计吗?它不起作用。我已经解决了这个问题(没有时间调查这个问题),但我会回来的,因为我决心让它的工作!
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.
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.
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; }
}