简体   繁体   English

查询或合并一对多关系,而无需重复结果条目

[英]Querying or Merging One-to-Many Relationship without Duplicating Entries of the Result

I have 2 very large data sets. 我有2个非常大的数据集。 The first "Main Table" is a unique list. 第一个“主表”是唯一列表。 The second "Reference Table" is a one-to-many relationship with the "Main Table". 第二个“参考表”与“主表”是一对多关系。

Objective: Query with filters across both data sets but display the results without duplicating the entries of the "Main Table". 目标:使用两个数据集的过滤器进行查询,但显示结果而无需重复“主表”的条目。 I don't want the user to scroll through 200 rows of the same line item just because the fields of the one-to-many "Reference Table" are highly unique. 我不希望用户仅在一对多“参考表”的字段中具有很高的唯一性,就可以滚动浏览同一订单项的200行。

Are there any techniques to deal with this? 有什么技巧可以解决这个问题?

All I can think of is sequentially appending the rows of the "Reference Table" as Columns to the "Main Table", like; 我能想到的就是将“参考表”的行作为列顺序追加到“主表”中,例如: a_1, b_1, a_2, b_2,... a_1,b_1,a_2,b_2,...

Here is an example of the data sets: data 这是数据集的示例: 数据

I'm working with C#, ASP.NET, SQL Server 2016, Entity Framework 我正在使用C#,ASP.NET,SQL Server 2016,实体框架

You can get a lot of functionality by splitting your query into two parts: 通过将查询分为两部分,您可以获得很多功能:

using (MainContext ctx = new MainContext()) {
  var rows =  from main in ctx.Main
              select new 
              {
                Main = main,
                References = from r in ctx.References
                             where r.Id == main.Id
                             select r
              }

  // Do something with your rows
}

This will send one query to the DB and return one entity per row in Main , with a separate property that will contain all of that Main row's metadata. 这将向数据库发送一个查询,并在Main每行返回一个实体, Main实体具有单独的属性,该属性将包含该Main行的所有元数据。

IF you send this back to client using, for example, a WebAPI call, the data on the client would look like: 如果使用(例如)WebAPI调用将其发送回客户端,则客户端上的数据如下所示:

[{
  main: { id: ..., x: ..., y: ..., z: ...},
  references: [{ a: ..., b: ... }, {a: ..., b: ... }, ... ]
},
...,
]

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

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