在 DataTable 上分组

Group by on DataTable

DataTable1

DataTable2

我要找的是:

第 1 步 - 从 DataTable1 中获取 colGroup 为 YES 或 OK 的 columnName。

步骤 2- 查询 DataTable2,并对从 DataTable1 返回的列进行分组(步骤 1)

这很棘手,但可以使用动态 Linq 库来完成。 using System.Linq.Dynamicusing Newtonsoft.Json.

void Main()
{
    var dataTable1 = new[] { 
        new DataTable1 { columnName = "OriginCompany", colGroup = "YES" },
        new DataTable1 { columnName = "OriginAddress", colGroup = "YES" },
        new DataTable1 { columnName = "OriginZip", colGroup = "OK" },
        new DataTable1 { columnName = "OriginState", colGroup = "NO" }};

    var dataTable2 = new[] {
        new DataTable2 { OriginCompany = "Company1", OriginAddress = "PAdds2_1", OriginZip = "10001", OriginState = "MI" }, 
        new DataTable2 { OriginCompany = "Company1", OriginAddress = "PAdds2_1", OriginZip = "10001", OriginState = "MI" },
        new DataTable2 { OriginCompany = "Company2", OriginAddress = "PAdds2_2", OriginZip = "10002", OriginState = "AZ" },
        new DataTable2 { OriginCompany = "Company3", OriginAddress = "PAdds2_3", OriginZip = "10003", OriginState = "MI" },
        new DataTable2 { OriginCompany = "Company3", OriginAddress = "PAdds2_3", OriginZip = "10003", OriginState = "UT" }};

    // Select the columns that will form the group-by
    var groupByColumns = dataTable1
        .Where(g => g.colGroup == "YES" || g.colGroup == "OK")
        .Select(g => g.columnName);

    // Use the dynamic linq GroupBy function
    var groupedData = dataTable2
        .GroupBy($"new({string.Join(",", groupByColumns)})", "it")
        .Select("new(Key as TheKey, it as MatchedRows)");
    
    foreach (var grp in groupedData)
    {
        // Using dynamic objects is not very fun, but the Newtonsoft Json 
        // library makes it bearable. Maybe there's a better way?
        var key = ((dynamic)grp).TheKey;
        Console.WriteLine($"Group: {JsonConvert.SerializeObject(key)}");
        
        // Again using Newtonsoft to convert dynamic objects to typed objects
        var rows = ((dynamic)grp).MatchedRows;
        var matches = (List<DataTable2>)JsonConvert.DeserializeObject<List<DataTable2>>(JsonConvert.SerializeObject(rows));
        foreach (var match in matches)
        {
            Console.WriteLine($"---> Value: {match}");
        }       
    }
}

public class DataTable1
{
    public string columnName { get; set; }
    public string colGroup  { get; set; }
}

public class DataTable2
{
    public string OriginCompany { get; set; }
    public string OriginAddress { get; set; }
    public string OriginZip { get; set; }
    public string OriginState { get; set; }
    public override string ToString() { return $"OriginCompany:{OriginCompany}, OriginAddress:{OriginAddress}, OriginZip:{OriginZip}, OriginState:{OriginState}"; }
}

这将 return 以下内容,我相信这是您想要的输出:

Group: {"OriginCompany":"Company1","OriginAddress":"PAdds2_1","OriginZip":"10001"}
---> Value: OriginCompany:Company1, OriginAddress:PAdds2_1, OriginZip:10001, OriginState:MI
---> Value: OriginCompany:Company1, OriginAddress:PAdds2_1, OriginZip:10001, OriginState:MI
Group: {"OriginCompany":"Company2","OriginAddress":"PAdds2_2","OriginZip":"10002"}
---> Value: OriginCompany:Company2, OriginAddress:PAdds2_2, OriginZip:10002, OriginState:AZ
Group: {"OriginCompany":"Company3","OriginAddress":"PAdds2_3","OriginZip":"10003"}
---> Value: OriginCompany:Company3, OriginAddress:PAdds2_3, OriginZip:10003, OriginState:MI
---> Value: OriginCompany:Company3, OriginAddress:PAdds2_3, OriginZip:10003, OriginState:UT