简体   繁体   English

C#实体框架:选择后获取子项的父级

[英]C# Entity Framework: Get the Parent of the Children after a Select

I'm trying to 'keep' the reference to the parent in the object after a query to EF starting by the parent entity while filtering on the both parent and children and returning the children. 在父进程开始查询EF之后,我正在尝试'保持'对象中父对象的引用,同时对父对象和子对象进行过滤并返回子对象。 But those children records i'm returning need to maintain their reference back to the parent. 但是我回来的那些孩子记录需要保持他们对父母的引用。

In the example below, the parent is available 'up until the select' is performed, and then the children are returned... but the reference 'back' to the parent is lost. 在下面的示例中,父级可用“直到执行选择”,然后返回子级...但是对父级的引用“返回”将丢失。 HOW can i not loose that object of the parent??? 我怎么能不松开父母的那个对象???

so what i need is that the return result of Table2Model, still holds reference back to parent in the MODEL.. but after the select statement that reference is lost. 所以我需要的是Table2Model的返回结果,仍然将引用保留在MODEL中的父级..但是在引用丢失的select语句之后。

if i add this 如果我加上这个

result.ForEach((t2) =>
  {
    t2.Table1 = db.Table1Model.SingleOrDefault(t1 => t1.Table1Id == t2.Table1Id);
  });

it fixes my problem... but i was wondering if there was some way to retain the parent reference in the original call. 它修复了我的问题......但我想知道是否有某种方法可以保留原始调用中的父引用。 Doing this in two steps doubles the work effort of the query. 分两步执行此操作会使查询的工作量增加一倍。

(db below is the database context) (下面的数据库是数据库上下文)

public class Table1Model {
    public Table1Id {get; set;}
    public Name {get; set;}
    public Desc {get; set;}
    public virtual ICollection<Table2Model> Table2Models {get; set;}
}

public class Table2Model {
    public Table2ModelId {get; set;}
    public SpecialData1 {get; set;}
    public SpecialData2 {get; set;}
    public SpecialData3 {get; set;}
    public Table1Id {get; set;}
    public virtual Table1Model Table1 {get; set;}
}

public class List<Table2Model> DoWork(sampleRequest request){

    var result = db.Table1Model
        .Where(t => t.Name = request.Name)
        .Select(t => t.Table2Models.OrderByDescending(n => n.Table2ModelId).FirstOrDefault())
        .OrderBy(t2 => t2.SpecialData1)
        .Take(5)
        .ToList();

    return result;
}

Perhaps I'm missing something here, but why not project into a different model and include both pieces. 也许我在这里遗漏了一些东西,但为什么不投射到不同的模型并包括两个部分。

public class TheModel
{
    public Table1Model Table1Model { get; set; }
    public Table2Model Table2Model { get; set; }
}

var result = db.Table1Model
    .Where(t => t.Name = request.Name)
    .Select(t => new TheModel {
        Table1Model = t,
        Table2Model = t.Table2Models
            .OrderByDescending(n => n.Table2ModelId)
            .FirstOrDefault())
    .OrderBy(t2 => t2.Table2Model.SpecialData1)
    .Take(5)
    .ToList();

I'd actually suggest you break down TheModel into just the specific properties you need and be a little more granular with the request, but I don't know what exactly you need to can't give a more specific example. 我实际上建议你将TheModel分解为你需要的特定属性,并且对请求更加细化,但我不知道你需要什么,不能给出一个更具体的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM