Linq查询产生不正确的结果

时间:2016-11-22 10:41:13

标签: c# json linq

考虑以下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"} 包含以下两项:

{{1}}

最终我会在列表中有更多项目,但只是想弄清楚为什么更多linq查询不能正常工作我已将其缩小到只是项目

3 个答案:

答案 0 :(得分:3)

I created a fiddle,可以更轻松地传达可用数据和预期结果。

当我理解正确时,你想得到所有产品的清单,列在fillTable中,然后从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"
      }
    ]
  }
]

答案 1 :(得分:1)

我不是很确定,但是这样会有用吗?

       var result = (from a in (from b in filledTable join c in distinctList on b[0].SerialNumber equals c.Field<string>("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),
                          ImportTable = d.Select(w => w.c.Table.AsEnumerable()
                          .Where(y=>y.Field<string>("SERIAL NUMBER") == d.Key.ToString())
                          .GroupBy(y => y.Field<string>("SERIAL NUMBER")).Select(z => z.First()).CopyToData‌​Table())
                      });

答案 2 :(得分:1)

以下是可以使用的简化查询:

>>> fexdata = {'device': ['2248_1', '2248_2', '2248_3'], 'fexprefix': ['Eth101/1/', 'Eth102/1/', 'Eth103/1/'], 'parent': ['5548_1', '5548_2', '5548_3']}
>>> destdevice = '2248_1'
>>> if destdevice in fexdata['device']:
...     parentindex = fexdata['device'].index(destdevice)
...     deviceparent = fexdata['parent'][parentindex]
...
>>> deviceparent
'5548_1'

它应该等同于所需的输出,并处理原始查询处理的大多数奇怪的数据组合。