DataTable列值分组和排序

时间:2017-11-24 05:26:24

标签: c# linq sorting datatable grouping

DataTable的

rows.map ( ( record ) => { try { (async () => { let col = await client.query(`SELECT * FROM customers`); })(); } catch (err) { console.log(err); } }); 包含3种类型的内容:

  1. 数字
  2. 字母数字
  3. 字母
  4. DataTable的

    ColumnA包含日期格式" YYYYMMDD "。

    我需要根据3种内容类型对数据进行分组,然后按日期升序排序。

    示例:

    ColumnA | ColumnB
    A123457 | 20171114 
    B246734 | 20171009
    1234544 | 20170808
    6789033 | 20171220
    ABBCDEE | 20180102
    A112233 | 20160202
    1212122 | 20171115
    NNNNNNN | 20171011
    

    根据ColumnB值进行分组并根据ColumnA值进行排序:

    ColumnA | ColumnB
    1234544 | 20170808
    1212122 | 20171115
    6789033 | 20171220
    
    A112233 | 20160202
    B246734 | 20171009
    A123457 | 20171114
    
    NNNNNNN | 20171011
    ABBCDEE | 20180102
    
    

    我们将成为LINQ解决方案吗?

1 个答案:

答案 0 :(得分:1)

下面不是确切的代码,通过以下方式可以达到预期的效果。

//sort the date and put into same list
CultureInfo provider = CultureInfo.InvariantCulture;
                    myList.ForEach(i => { i.ColumnB = DateTime.ParseExact(i.ColumnB, "YYYYMMDD", provider);});

//check for regex match and add to list
                    mynumericlist = myList.Where(x => Regex.IsMatch(x.columnA, @"^\d$")).ToList().OrderBy(x=>x.ColumnB);
                    myalphalist = myList.Where(x => Regex.IsMatch(x.columnA, @"^[a-zA-Z]*$")).ToList().OrderBy(x => x.ColumnB);
                    myalpnumlist = myList.Where(x => Regex.IsMatch(x.columnA, @"^[a-zA-Z0-9]*$")).ToList().OrderBy(x => x.ColumnB);

                    //then combine all