简体   繁体   English

Linq EF将父级拆分为多个父级

[英]Linq EF Split Parent into multiple Parents

Using Entity Framework to query a database with a Parent table and Child table with a 1-n relationship: 使用实体框架查询具有1-n关系的Parent表和Child表的数据库:

public class Parent {
    public int id { get; set; }
    public IList<Child> Children { get; set; }
}

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

Using EF, here's a quick sample query: 使用EF,这是一个快速的示例查询:

var parents = context.Parents;

Which returns: 哪个返回:

parent id = 1, children = { (id = 1), (id = 2), (id = 3) }

What we need is for this to flatten into a 1-1 relationship, but as a list of parents with a single child each: 我们需要的是将这种关系扁平化为1-1关系,但作为每个有一个孩子的父母的清单:

parent id = 1, children = { (id = 1) }
parent id = 1, children = { (id = 2) }
parent id = 1, children = { (id = 3) }

We're using an OData service layer which hits EF. 我们正在使用点击EF的OData服务层。 So performance is an issue -- don't want it to perform a ToList() or iterate the entire result for example. 因此,性能是一个问题-例如,不希望它执行ToList()或迭代整个结果。


We've tried several different things, and the closest we can get is creating an anonymous type like such: 我们尝试了几种不同的方法,而最接近的方法是创建一个匿名类型,例如:

var results = from p in context.Parents
              from c in p.Children
              select new { Parent = p, Child = c }

But this isn't really what we're looking for. 但这并不是我们真正想要的。 It creates an anonymous type of parent and child, not parent with child. 它创建匿名类型的父级和子级,而不是父级和子级。 So we can't return an IEnumerable<Parent> any longer, but rather an IEnumerable<anonymous> . 因此,我们不能再返回IEnumerable<Parent> ,而是返回IEnumerable<anonymous> The anonymous type isn't working with our OData service layer. 匿名类型不适用于我们的OData服务层。

Also tried with SelectMany and got 3 results, but all of Children which again isn't quite what we need: 还尝试了SelectMany并获得了3个结果,但所有的Children仍然不是我们所需要的:

context.Parents.SelectMany(p => p.Children)

Is what we're trying to do possible? 我们正在尝试做的可能吗? With the sample data provided, we'd want 3 rows returned -- representing a List each with a single Child. 使用提供的样本数据,我们希望返回3行-代表一个带有单个Child的List。 When normally it returns 1 Parent with 3 Children, we want the Parent returned 3 times with a single child each. 通常情况下,它返回1个带3个孩子的父母,我们希望父母返回3次,每个孩子带一个孩子。

Your requirements don't make any sense, the idea behind how EF and LINQ work is not those repetitive info like SQL does. 您的需求没有任何意义,EF和LINQ如何工作背后的想法不是像SQL那样的重复信息。 But you know them better and we don't know the whole picture, so I will try to answer your question hoping I understood it correctly. 但是您对它们的了解更好,而我们并不了解整个情况,因此我将尝试回答您的问题,希望我理解正确。

If like you said, your problem is that IEnumerable<anonymous> doesn't work with your OData service layer, then create a class for the relationship: 如果像您说的那样,您的问题是IEnumerable<anonymous>不适用于您的OData服务层,然后为该关系创建一个类:

public class ParentChild {
    public Parent Parent { get; set; }
    public Child Child { get; set; }
}

And then you can use in in your LINQ query: 然后可以在LINQ查询中使用:

var results = from p in context.Parents
              from c in p.Children
              select new ParentChild { Parent = p, Child = c }

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

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