简体   繁体   English

如何对从 LINQ 查询从 SQL Server 返回的相同结果集的结果进行分组和联接?

[英]How do i group and join results of the same result set returned from SQL Server from LINQ query?

I have the following data structure我有以下数据结构

在此处输入图片说明

In a Customer report, there are multiple orders from different clients and each order there are one to many items.在客户报告中,有来自不同客户的多个订单,每个订单有一对多的项目。 When I execute my stored procedure, the data it returns in the result set has the above structure (note: there are duplicates in the query because one order has many items).当我执行我的存储过程时,它在结果集中返回的数据具有上述结构(注意:查询中有重复项,因为一个订单有多个项目)。 There is a column called ReportSequence this is the order of appearance of the record on the report.有一个名为ReportSequence的列,这是记录在报告上的出现顺序。

public class CustomerReport
{
    public int CustomerId { get; set; }     //maps to the CustomerId column in SQL Query result
    public string FirstName { get; set; }   //maps to the FName column
    public string LastName { get; set; }    //maps to the LName column
    public DateTime? ReportDate { get; set; }  //Generated at object creation
    public List<SaleOrder> SalesOrders { get; set; }  //see SaleOrder class

    public CustomerReport()
    {
        SalesOrders = new List<SaleOrder>();
    }
}
public class SaleOrder
{
    public int? SaleOrderId { get; set; }       //maps to the SalesOrderId column in sql query result 
    public string SaleOrderNumber { get; set; }  // maps to SalesOrderNumber column

    public List<SalesOrderItem> OrderItems { get; set; }  //see SalesOrderItem class
    public int? SaleOrderTypeId { get; set; }     //maps to SalesOrderTypeId column
    public int? ReportSequenceNo { get; set; } //the sequence order 1,2,3... how each Sales Order appears on the report

    public SaleOrder()
    {
        OrderItems = new List<SalesOrderItem>();
    }
}
public class SalesOrderItem
{
    public int? ItemTypeId { get; set; }  //maps to ItemTypeId column in sql query result 
    public int? ItemId { get; set; }      // maps to ItemId column
    public string ItemName { get; set; }  //maps to ItemName column
}

Using the result returned by query execution (show on Excel screenshot), I need to generate the following JSON structure:使用查询执行返回的结果(显示在 Excel 屏幕截图上),我需要生成以下 JSON 结构:

    {
        "CustomerId" : "10001",
        "FirstName" : "Nicholas",
        "LastName" : "Cage",
        "ReportDate" : "toays date",
        SalesOrders: [
            { 
                SaleOrderId : 6 , 
                SaleOrderNumber : "EB100X100" ,
                SaleOrderTypeId : 102, 
                ReportSequenceNo : 1, 
                SalesOrderItems : [
                    { ItemTypeId : NULL, ItemId :  NULL, ItemName : Null }
                ]  
            },
            ...
        ]
    }

The method I followed is:我遵循的方法是:

First I have tried to build two sets of data from one set首先,我尝试从一组数据构建两组数据

  1. The original result set was grouped by SalesOrderId原始结果集按SalesOrderId分组
  2. The above was joined with the original以上是与原文结合的

But joining failed ... could not join them....但是加入失败……无法加入他们……

How do I build this JSON structure from Linq using above resultset and classes?如何使用上述结果集和类从 Linq 构建此 JSON 结构?

all right, what you need here is the proper grouping of the source data.好的,这里你需要的是对源数据进行正确的分组。

allRecords.GroupBy(a=>a.CustomerId).Select(b=> new CustomerReport(b.Key){ 
SalesOrders = b.Values.GroupBy(c=>c.SaleOrderId).Select(d=> new SaleOrder(d.Key).........)}

It is not complete but I hope gives you a hint不完整,但希望能给你一个提示

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

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