在连接两个数据表之后,在内连接之后没有获得预期的行

时间:2018-02-19 12:47:10

标签: c# linq

我在复合密钥上加入两个数据表,即帐号。和金额相关,但根据我已经附上的图片我在加入后只获得一行,但我应该得到2行。

var query = from dataRows1 in clearingDTAlias.AsEnumerable()
            join dataRows2 in excelDT.AsEnumerable()
            on new
            {
                Account_No = dataRows1.Field<string>("Account_No"),
                Amount = dataRows1.Field<string>("Withdraw_Amount_Requested")
            } equals new
            {
                Account_No = dataRows2.Field<string>("Account_No"),
                Amount = dataRows2.Field<string>("Amount")
            }
            select joinDT.LoadDataRow(new object[]
            {
                dataRows2.Field<string>("Account_No"),
                dataRows2.Field<string>("Amount"),
                dataRows2.Field<string>("Code"),
                dataRows2.Field<string>("Row_No"),
            }, true);

if (query.Any())
{
    var query2 = query.GroupBy(test => test.Field<string>("Row_No")).Select(grp => grp.First()).ToList();
    if (query2.Any())
        joinDT = query2.CopyToDataTable();
    else
        joinDT = excelDT.Clone();
}

1 个答案:

答案 0 :(得分:0)

使用下面的group by子句从查询中获取分组的行。

var query2 = query.GroupBy(test => test.Field<string>("Row_No"))
                  .Select(grp => grp.Key).ToList()