C# Linq检索父对象

C# Linq检索父对象,c#,linq,C#,Linq,我正在使用entityframework,并且父对象MyObject已经存在于我的上下文中(并加载了childs) 公共类MyObject { 公共int id{get;set;} 公共字符串名称{get;set;} 公共IEnumerable子项{get;set;} } 公营儿童 { 公共int id{get;set;} 公共字符串名称{get;set;} 公共IEnumerable child2{get;set;} } 公营儿童2 { 公共int id{get;set;} 公共字符串名称{g

我正在使用entityframework,并且父对象
MyObject
已经存在于我的上下文中(并加载了childs)

公共类MyObject
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共IEnumerable子项{get;set;}
}
公营儿童
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共IEnumerable child2{get;set;}
}
公营儿童2
{
公共int id{get;set;}
公共字符串名称{get;set;}
}
我想在
Child2
属性上设置一个条件,保留父对象的实例

var myFiletredMyObject=myObject.child.Where(c=>c.child2.Any(c2=>c2.id==1))

这将返回一个
子对象的集合,如何获取父对象:
MyObject

考虑到EntityFramework将从数据库返回模型列表,您可以像这样筛选EF对象

var filterResult = myObject.Where(x => x.child.Any(a=>a.child2.Any(c2 => c2.id == 1))).ToList();
筛选结果的类型为MyObject

如果对象类型为MyObject,则可以使用三元运算符应用条件

var filterResult = myObject.child.Where(a => a.child2.Any(c2 => c2.id == 1)).ToList().Count > 0 ? myObject : null;
如果您遵循了,那么应该将表之间的关系编写为虚拟属性

在实体框架中,表的列由非虚拟属性表示;虚拟属性表示表之间的关系(一对多、多对多)

此外:最好将一对多描述为
ICollection
,而不是
IEnumerable
。这使您能够询问子项的数量,并添加子项,实体框架可以轻松地将其转换为SQL

因此,只需对代码进行一点更改,这些更改将为您提供相同的数据库(在我的示例中,我对属性进行了一点重命名,以使其更简单)


编辑,很抱歉,根据你的类结构,我看不到任何方法来实现这一点,因为
子对象
对其父对象没有任何线索。但是,
myObject
是否已经是您所追求的对象?@HimBromBeere事实上,我想这样做是为了在父对象中只保留一个子对象为什么您首先有两个子模型类?嗯,
var object=myObject完成。如果将MyObjor视为I枚举(作为EF的数据集),则该解决方案是好的。如果我找不到其他解决方案,这可能是一个解决办法。在我的例子中,myObject已经是myObject类型(来自上一个请求),它只返回我的初始对象(myObject),而不应用条件。如果条件满足,它将返回原始区域对象。否则它将返回null。我创建了一个模型基EntityFramework,因此已经遵循了此约定。我只是想用我的3个类做一个非常(太)简单的例子。谢谢你的回答,这就是我想要的,太好了!
public class Parent
{
    public int id { get; set; }
    public string name { get; set; }

    // every Parent has zero or more Children (one-to-many)
    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    public int id { get; set; }
    public string name { get; set; }

    // every Child is the child of exactly one Parent, using a foreign key
    public int ParentId {get; set;}
    public virtual Parent Parent {get; set;}

    // every Child has zero or more Child2 (one-to-many)
    public virtual ICollection<Child2> Children2 { get; set; }
}

public class Child2
{
    public int id { get; set; }
    public string name { get; set; }

    // every Child2 belongs to exactly one Child, using foreign key
    public int ChildId {get; set;}
    public virtual Child Child {get; set;}
}
public class FamilyDbContext
{
    public DbSet<Parent> Parents {get; set;}
    public DbSet<Child> Children {get; set;}
    public DbSet<Child2> Children2 {get; set;}
}
public override void OnModelCreating(DbModelBuilder modelBuilder);
{
    // configure the one-to-many between Parent and Child using the foreign key:
    modelBuilder.Entity<Parent>()
        .HasMany(parent => parent.Children)
        .HasRequired(child => child.Parent)
        .HasForeignKey(child => child.ParentId);

    // configure the one-to-many between Child and Child2 using foreign key
    // make sure that the table gets a proper name
    modelBuilder.Entity<Child>().ToTable("Children")
         .HasMany(child => child.Children2)
         .HasRequired(child2 => child2.Child)
         .HasForeignKey(child2 => child2.ChildId);

    // the one-to-many relations are configured. Set the last table name
    modelBuilder.Entity<Child2>().ToTable("Children2");
}
var myFilteredChildrenWithTheirParents = dbContext.Children
    .Where(child => ...)
    .Select(child => new
    {
        // select only the Child properties you really plan to use:
        Id = child.Id,
        Name = child.Name,

        Parent = new
        {   // again: select only the Parent properties you plan to use:
            Id = parent.Id,
            Name = parent.Name,
        },

        // no need for the foreign key, you already selected the value in Parent
        // ParentId = child.ParentId,
});