考虑以下linq查询

var result = from a in
                 from b in filledTable
                 join c in distinctList on b[0].SerialNumber equals c.Field("SERIAL NUMBER")
                 select new { b, c }
             group a by new { a.b[0].SerialNumber } into d
             select new
             {
               Id = d.Select(x => x.b[0].Id),
               SerialNumber = d.Select(x => x.b[0].SerialNumber),
               // This part is not producing the correct output.
               ImportTable = d.Select(w => w.c.Table
                                              .AsEnumerable()
                                              .GroupBy(y => y.Field("SERIAL NUMBER"))
                                              .Select(z => z.First())
                                              .CopyToData‌​Table())
             };

我的linq查询中的filledTableList<dynamic>,由sproc返回的值填充,而distinctListList<DataRow>,我将来自DataTable的值区分如下:
List<DataRow> distinctList = dt.AsEnumerable().Distinct(DataRowComparer.Default).ToList();

我的Linq查询产生以下JSON
[
{
"FilledTableList":[
  [
    {
      "Id":[
        2
      ],
      "SerialNumber":[
        "1073410"
      ],
      "ImportTable":[
        [
          {
            "SERIAL NUMBER":"1073410",
            "PRODUCT TYPE":"Product A"
          },
          {
            "SERIAL NUMBER":"1073411",
            "PRODUCT TYPE":"Product B"
          }
        ]
      ]
    },
    {
      "Id":[
        -1
      ],
      "SerialNumber":[
        "1073411"
      ],
      "ImportTable":[
        [
          {
            "SERIAL NUMBER":"1073410",
            "PRODUCT TYPE":"Proeduct A"
          },
          {
            "SERIAL NUMBER":"1073411",
            "PRODUCT TYPE":"Product B"
          }
        ]
      ]
    }
  ]
]
}]

但是我想要下面的JSON输出
[
{
"FilledTableList":[
  [
    {
      "Id":[
        2
      ],
      "SerialNumber":[
        "1073410"
      ],
      "ImportTable":[
        [
          {
            "SERIAL NUMBER":"1073410",
            "PRODUCT TYPE":"Product A"
          }
        ]
      ]
    },
    {
      "Id":[
        -1
      ],
      "SerialNumber":[
        "1073411"
      ],
      "ImporTable":[
        [
          {
            "SERIAL NUMBER":"1073411",
            "PRODUCT TYPE":"Product B"
          }
        ]
      ]
    }
  ]
]
}]

因此,ImportTable节点仅包含与上述FilleTabledList节点中的序列号匹配的信息。除此之外,其他所有内容似乎都可以按照Linq查询的预期运行。有人可以告诉我我要去哪里了吗

更新:

我的filledTable包含以下两项:
{ Id = 2, SerialNumber = "1073410"}
{ Id = -1, SerialNumber = "1073411"}

最终,我将在列表中添加更多项目,但只是为了弄清楚为什么更多的linq查询不起作用,我将其范围缩小到了仅项目

最佳答案

created a fiddle,这使得更容易传达可用数据和预期结果。

当我正确理解它时,您希望获得在filledTable中列出的所有产品的列表,然后从dataTable中找到具有相同序列号的所有元素。

如果正确,那么LINQ查询必须是:

    var result = filledTable.GroupJoin(distinctList, product => product.SerialNumber, row => row.Field<string>("SERIAL NUMBER"), (Product, Rows) => new { Product, Rows })
                            .Select(group => new
                                    {
                                        Id = group.Product.Id,
                                        SerialNumber = group.Product.SerialNumber,
                                        ImportTable = group.Rows.CopyToDataTable()
                                    });

结果将是
[
  {
    "Id": 2,
    "SerialNumber": "1073410",
    "ImportTable": [
      {
        "SERIAL NUMBER": "1073410",
        "PRODUCT TYPE": "Product A"
      }
    ]
  },
  {
    "Id": -1,
    "SerialNumber": "1073411",
    "ImportTable": [
      {
        "SERIAL NUMBER": "1073411",
        "PRODUCT TYPE": "Product B"
      }
    ]
  }
]

10-04 16:36