按数据表分组

Posted

技术标签:

【中文标题】按数据表分组【英文标题】:Group by on DataTable 【发布时间】:2022-01-17 22:35:09 【问题描述】:

数据表1

数据表2

我正在寻找的是:

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

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

【问题讨论】:

请更具体地说明您的要求,甚至更多 - 您已经尝试过的内容。否则,您将关闭您的问题。 【参考方案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"; 

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

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

【讨论】:

@Carlos 感谢您的帮助。 .GroupBy 给出以下错误:CS0411 无法从用法中推断方法 'Enumerable.GroupBy(IEnumerable, Func, IEqualityComparer?)' 的类型参数。尝试明确指定类型参数。 @Carlos,感谢您花时间回答这个问题并提供帮助。安装 NuGet 包也有帮助。 code 使用 System.Linq.Dynamic.Core; @Zeeshan 是的,我使用的这个 GroupBy 不是你的常规 GroupBy :-),它是 System.Linq.Dynamic 库中提供的。

以上是关于按数据表分组的主要内容,如果未能解决你的问题,请参考以下文章

按数据表分组

按索引分组的数据框

oracle怎么按个数分组

核心数据按年份分组并按日期排序

按每周一分组数据未按预期工作

按外键和日期分组数据,按日期汇总