来自数据表的动态 Linq 查询 Table
Dynamic Linq Query from Datatable for Pivot Table
我已经为这个问题苦苦挣扎了一段时间,想知道是否可以得到一些帮助。
我有以下物品:
Datatable inputdata = //Data from large scale SQL query
var rowvals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 0 && x.Field<long>("VALUEFIELD") == 0
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
var colvals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 1 && x.Field<long>("VALUEFIELD") == 0
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
var datavals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 1 && x.Field<long>("VALUEFIELD") == 1
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
Rowvals 是我们要在行中筛选的 table 的属性,colvals 是数据透视表 table 的列值,datavals 是数据总和
我想做的是创建我自己的自定义枢轴 table 例程,使我能够以更复杂的方法进行过滤和分层。
我能够获取一行的特定不同值:
foreach(object val in rowvals){
var distinctValues = dsValues.AsEnumerable()
.Select(row => new {
attribute1_name = row.Field<string>(val)
})
.Distinct();
}
我很难过滤出每个值的组合(对于可变数量的行和列)。不仅如此,我还需要 select 每个值列的所有值,以便我可以按照我认为合适的方式对其进行聚合。
如有帮助将不胜感激!
谢谢
乔恩
您的输入数据的样子不是很清楚;因为你想构建 pivot table 我假设它可以像那样显示为数据集(如果不是它应该转换为表格视图 acceptable 用于 pivot 操作):
col1 | col2 | val1
-------------------
C1 | R1 | 1
C1 | R2 | 2
C1 | R1 | 3
C2 | R1 | 4
和所需的枢轴 table 是类似的东西(让我们对枢轴 table 列使用 'col1' 值,对行使用 'col2' 值,对 val1 的 SUM值):
| C1 | C2
-----------------
R1 | 4 | 4
R2 | 2 | 0 (or empty)
像这样的枢轴数据 table 可以通过 NReco PivotData 库轻松计算,只需几行代码(免责声明:我是该库的作者):
// group and calculate measures
var pivotData = new PivotData(
new string[] {"col1","col2"},
new SumAggregatorFactory("val1"),
new DataTableReader(t) );
// get pivot table model for accessing columns/rows/values in easy way
var pivotTable = new PivotTable(
new []{"col2"}, // row dimension(s)
new []{"col1"}, // column dimension(s)
pivotData );
var rowLabels = pivotTable.RowKeys;
var colLabels = pivotTable.ColumKeys;
var cellValue = pivotTable[0, 0].Value; // R1 + C1: 4
var grandTotal = pivotTable[null,null].Value; // 10
您还可以为 rows/columns 指定多个列,并可以计算多个度量。有什么不明白的可以随时联系我。
我已经为这个问题苦苦挣扎了一段时间,想知道是否可以得到一些帮助。
我有以下物品:
Datatable inputdata = //Data from large scale SQL query
var rowvals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 0 && x.Field<long>("VALUEFIELD") == 0
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
var colvals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 1 && x.Field<long>("VALUEFIELD") == 0
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
var datavals = (from x in reportattrs.AsEnumerable()
where x.Field<long>("COLFORREPORT") == 1 && x.Field<long>("VALUEFIELD") == 1
orderby x.Field<long>("ORDERDISPLAY")
select new { Name = x.Field<object>("FIELDNAME") });
Rowvals 是我们要在行中筛选的 table 的属性,colvals 是数据透视表 table 的列值,datavals 是数据总和
我想做的是创建我自己的自定义枢轴 table 例程,使我能够以更复杂的方法进行过滤和分层。
我能够获取一行的特定不同值:
foreach(object val in rowvals){
var distinctValues = dsValues.AsEnumerable()
.Select(row => new {
attribute1_name = row.Field<string>(val)
})
.Distinct();
}
我很难过滤出每个值的组合(对于可变数量的行和列)。不仅如此,我还需要 select 每个值列的所有值,以便我可以按照我认为合适的方式对其进行聚合。
如有帮助将不胜感激!
谢谢 乔恩
您的输入数据的样子不是很清楚;因为你想构建 pivot table 我假设它可以像那样显示为数据集(如果不是它应该转换为表格视图 acceptable 用于 pivot 操作):
col1 | col2 | val1
-------------------
C1 | R1 | 1
C1 | R2 | 2
C1 | R1 | 3
C2 | R1 | 4
和所需的枢轴 table 是类似的东西(让我们对枢轴 table 列使用 'col1' 值,对行使用 'col2' 值,对 val1 的 SUM值):
| C1 | C2
-----------------
R1 | 4 | 4
R2 | 2 | 0 (or empty)
像这样的枢轴数据 table 可以通过 NReco PivotData 库轻松计算,只需几行代码(免责声明:我是该库的作者):
// group and calculate measures
var pivotData = new PivotData(
new string[] {"col1","col2"},
new SumAggregatorFactory("val1"),
new DataTableReader(t) );
// get pivot table model for accessing columns/rows/values in easy way
var pivotTable = new PivotTable(
new []{"col2"}, // row dimension(s)
new []{"col1"}, // column dimension(s)
pivotData );
var rowLabels = pivotTable.RowKeys;
var colLabels = pivotTable.ColumKeys;
var cellValue = pivotTable[0, 0].Value; // R1 + C1: 4
var grandTotal = pivotTable[null,null].Value; // 10
您还可以为 rows/columns 指定多个列,并可以计算多个度量。有什么不明白的可以随时联系我。