在 C#.net 中使用 LINQ 连接两个 CSV 文件的列

问题描述 投票:0回答:1

我正在做一个项目,其中包含两个 csv 文件并将它们转换为数据表,我想加入它们并将其分配给一个新文件。只有 BINBBL 列是固定的,其他列可能会发生变化。我不太习惯 linq。结果可能是这样的:

    if radioButton1.checked{
    join all the columns in dt1; //dt1 = dt1 + dt2
    }
    else if radioButton2.checked{
    join all the columns in dt2; //dt2 = dt2 + dt1
    }

现在,如果启用了 radioButton1,则 dt1 和 dt2 具有相同的列,例如 BBL,在 dt1 中有 4 行 BBL,在 dt2 中有 5 行,其中前 4 个匹配项存在于 dt1 中,但第 5 个匹配项存在是新的,那么它将在 dt1 中添加这一行。对于 radioButton2,反之亦然。

我认为以下类型的 sql 查询可能会有所帮助:

SELECT * FROM dt1,dt2 GROUP BY BBL

我确信它会添加 dt1 和 dt2 中常见的行,但是如果 dt2 有额外的行那么它是否会工作,我不知道。

c# sql linq join datatable
1个回答
1
投票

看这个

var ret = from x in dt1.AsEnumerable()
    join y in dt2.AsEnumerable() on x.Field<int>("ID") equals y.Field<int>("ID") into z
    from y in z.DefaultIfEmpty()
    select new
    {
        ID = x.Field<int>("ID"),
        VAL = x.Field<string>("VAL")
     };
© www.soinside.com 2019 - 2024. All rights reserved.