【问题标题】:How to filter data from datatable according to columns suffix如何根据列后缀过滤数据表中的数据
【发布时间】:2016-03-08 11:30:31
【问题描述】:

如果我有一个具有相同数据类型 int 的 columns with suffix "_" 的数据表。

如何通过 LINQ 获取所有这些列等于某个值的数据,例如 1

前:

emp_num  day_   penalty_   role_  abscence

23       12       1          2       true
24        1       1          1       true
76        2       5          1       false
55        1       1          1       false

我想得到这样的结果:

24        1       1          1       true
55        1       1          1       false

因为数据表的列数是可变的,我想要一些通用的方法来以某种方式投影这些列:

DT.AsEnumerable().Where(all columns with suffix "-" = 1)

【问题讨论】:

    标签: c# asp.net linq datatable


    【解决方案1】:

    给你:

    var columns = DT.Columns.Cast<DataColumn>()
        .Where(c => c.DataType == typeof(int) && c.ColumnName.EndsWith("_"))
        .ToList();
    var query = DT.AsEnumerable().Where(row => columns.All(c => row.Field<int>(c) == 1));
    

    columns 变量不是很需要。 ToList调用可以去掉,列查询可以嵌入到行查询中,但是为了效率,最好提前提取成一个列表,如上面sn-p。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2023-03-13
      相关资源
      最近更新 更多