简体   繁体   English

C#SQL父子表选择查询帮助

[英]C# SQL parent child table select query help

I have two tables that I want to fetch data from. 我有两个表要从中获取数据。

Lets call them "Parent" and "Children". 让我们称他们为“父母”和“孩子”。 The "Parent" table has many records in the "Children" table. “父”表在“子”表中有许多记录。 (One to Many) (一对多)

My problem is I want to find an efficient method for displaying the data the two tables contain to my ASP.NET MVC application. 我的问题是我想找到一种有效的方法来显示两个表包含在我的ASP.NET MVC应用程序中的数据。 My approach is to select all records from "Parent" then loop through each one selecting the data from "Children". 我的方法是从“Parent”中选择所有记录,然后遍历每个记录,从“Children”中选择数据。

So I need help designing a query for SQL Server 05 that can get the data out for C# more efficiently, I was planning of creating type classes and the "Parent" class would have a "List" that in my View I can loop through outputting the values. 所以我需要帮助设计SQL Server 05的查询,它可以更有效地获取C#的数据,我计划创建类型类,“Parent”类将有一个“List”,在我的View中我可以循环输出价值。

public class Parent_Type
{
    public string Name { get; set; }
    public List<Children_Type> Children { get; set; }
}


public class Children_Type
{
    public string Name { get; set; }
}

The data needs to be displayed like: (The name of the "Parent" record then a list of "Children" records") 数据需要显示如下:(“父”记录的名称,然后是“子”记录列表“)

"Parent 123" “家长123”

  • "Child 1" “孩子1”
  • "Child 2" “孩子2”
  • "Child 3" “孩子3”

"Parent 124" “父母124”

  • "Child 1" “孩子1”
  • "Child 2" “孩子2”
  • "Child 3" “孩子3”

I'd pull all of the data at once and then run through it programmatically. 我会立即提取所有数据,然后以编程方式运行它。

SELECT Parent.Id, Parent.Name FROM Parent 
LEFT OUTER JOIN
SELECT Child.Id AS ChildId, Child.Name AS ChildName FROM Child
ON
Child.ParentId = Parent.Id
ORDER BY Parent.Name

When you get the data back, the parents will be repeated so you'll need to filter it down. 当您获得数据时,将重复父项,因此您需要对其进行过滤。 You could do this via LINQ by providing a parent comparison or via a for loop. 您可以通过提供父比较或通过for循环通过LINQ执行此操作。 Itereate through the list collapsing on unique parent as each record will, in fact, represent a child or a childless parent. Itereate通过列表折叠在唯一的父级上,因为每条记录实际上代表一个孩子或无子女的父母。

Information about LINQ's "distinct" is available here . 有关LINQ“distinct”的信息,请点击此处

Well, you can always write a query that will return a JOIN across both tables: 好吧,你总是可以编写一个查询,它将在两个表中返回一个JOIN:

  SELECT Parent.ID, Parent.Field2, Parent.Field3,
         Child.ID, Child.Value2, Child.Value3
  FROM
     Parent
  INNER JOIN (or: LEFT OUTER JOIN)
     Child ON Parent.ID = Child.ParentID

but in this case, you'll get a "flattened" rowset back, in which the values for the parents are repeated for each of their child elements, obviously (standard SQL join behavior). 但是在这种情况下,你会得到一个“扁平”的行集,其中父元素的值对于每个子元素都是重复的(显然是标准的SQL连接行为)。

As has been pointed out in the comments, if you use the INNER JOIN , of course, you'll only get back parents and their child records - parents without children will be left out. 正如评论中指出的那样,如果你使用INNER JOIN ,当然,你只会收回父母和他们的孩子记录 - 没有孩子的父母将被排除在外。 If you want those, too (with their child columns set to NULL), use LEFT OUTER JOIN instead of INNER JOIN . 如果你也想要那些(将其子列设置为NULL),请使用LEFT OUTER JOIN而不是INNER JOIN

You're second option would be to have an ADO.NET query that returns two results, basically - something like 你的第二个选择是有一个返回两个结果的ADO.NET查询,基本上就是这样的

 SELECT Parent.ID, Parent.FIeld2, Parent.Field3 ;
 SELECT Child.ID, Child.ParentID, Child.Value2, Child.Value3;

but then you'll need to parse two result sets from the response, associate the child records with their respective parents, and do some more work. 但是你需要从响应中解析两个结果集,将子记录与他们各自的父母相关联,并做更多的工作。

Your third option would be to create a "FOR XML" query in SQL Server that would return an XML document representation the hierarchy of Parents and Child records in a single XML document. 您的第三个选择是在SQL Server中创建一个“FOR XML”查询,该查询将在单个XML文档中返回XML文档表示父级和子级记录的层次结构。

SELECT
    Parent.ID, Parent.Field2, Parent.Field3,
    (SELECT 
       Child.ID, Child.Value2, Child.Value3
     FROM Child
     WHERE ParentID = Parent.ID FOR XML AUTO, TYPE, ELEMENTS)
FROM
    Parent
FOR XML 
    AUTO, ROOT('Root'), ELEMENTS

Whichever works best for you - take your pick! 无论哪种方式最适合您 - 请选择!

Marc

If you are looking to store and retrieve heirarchical data, you can do this in a single table, have a look at SQL Server CTE Expressions (CTE). 如果您希望存储和检索层次数据,可以在单个表中执行此操作,查看SQL Server CTE表达式(CTE)。 an example here http://msdn.microsoft.com/en-us/library/ms190766.aspx 这里的一个例子http://msdn.microsoft.com/en-us/library/ms190766.aspx

You didn't really clearly asked a question you just mentioned some thoughts and I think that's why someone downvoted but a comment would have been nice. 你并没有真正清楚地问过你刚才提到的一些问题,我认为这就是为什么有人投票但评论会很好。 Any ways if you want to know what SQL to use checkout Select Parent Record With All Children in SQL 如果您想知道要使用的SQL检查的任何方法,请在SQL中选择包含所有子项的父记录

the other way (used by some ORM's) is to do two selects. 另一种方式(由一些ORM使用)是做两个选择。 one for all parents and one for all childeren and to then join them in the applications memory instead of on the database server. 一个用于所有父项,一个用于所有childeren,然后将它们连接到应用程序内存而不是数据库服务器上。

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

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