我有数据视图,我需要根据两列不同值的组合列表(比如ColumnA和ColumnB)获取行计数。我可以从这两列中获取不同值的String列表。我正在努力的部分是如何根据String列表中的每个项从dataview获取精确的行计数(到整数列表中)。我认为问题是ColumnA中的某些值可能不存在于ColumnB中,反之亦然。如果我应用基于ColumnA的行过滤器,则可能会错过某些值ColumnB这就是为什么我做了类似下面的操作。但我最终在我的整数列表中得到了额外的计数。如果我不这样做,那么我会错过一些值,导致总得分低于no。行。
请指教。我需要使用最终的String和Int值List来创建一个基本表,然后在excel中创建一个图表。
For x As Integer = 0 To DistinctColValueList.Count - 1
'' OR Condition Below Gives more count
'Dv.RowFilter = "ColumnA = '" & DistinctColValueList.ElementAt(x) & "' OR ColumnB = '" & DistinctColValueList.ElementAt(x) & "'"
Dv.RowFilter = "ColumnA = '" & DistinctColValueList.ElementAt(x) & "'"
DistinctIntValueList.Add(Dv.Count)
Next
Dim ClonedList As New List(Of Integer)
For Each item In DistinctIntValueList
ClonedList.Add(item)
Next
Dv.RowFilter = ""
DistinctIntValueList.Clear()
For y As Integer = 0 To DistinctColValueList.Count - 1
Dv.RowFilter = "ColumnB = '" & DistinctColValueList.ElementAt(y) & "'"
Dim Abs_Diff = Math.Abs(ClonedList.ElementAt(y) - Dv.Count)
DistinctIntValueList.Add(ClonedList.ElementAt(y) + Abs_Diff)
Next
更新:添加了示例表。
+-----------------------------+
|Item |Column_A |Column B |
| | | |
+-----------------------------+
| XYZ1 | Loc1 | Loc5 |
| XYZ2 | Loc13 | Loc6 |
| XYZ3 | Loc4 | Loc4 |
| XYZ4 | Loc6 | Loc10 |
| XYZ5 | Loc5 | Loc2 |
| XYZ6 | Loc5 | Loc6 |
| XYZ7 | Loc6 | Loc9 |
| XYZ8 | Loc4 | Loc10 |
| XYZ9 | Loc6 | Loc13 |
| XYZ10 | Loc2 | Loc10 |
+--------+----------+---------+
答案 0 :(得分:1)
问题的一个解决方案是将A列和B列中的值连接起来,其中A<> B,然后是Concat,它们是相同的,并进行计数。
使用提供的DataTable,我写了以下内容。它还具有立即获取不同值和计数的优势
Dim dt As New DataTable("Test")
dt.Columns.Add("Item", "".[GetType]())
dt.Columns.Add("Column_A", "".[GetType]())
dt.Columns.Add("Column_B", "".[GetType]())
dt.Rows.Add("XYZ1", "Loc1", "Loc5")
dt.Rows.Add("XYZ2", "Loc13", "Loc6")
dt.Rows.Add("XYZ3", "Loc4", "Loc4")
dt.Rows.Add("XYZ4", "Loc6", "Loc10")
dt.Rows.Add("XYZ5", "Loc5", "Loc2")
dt.Rows.Add("XYZ6", "Loc5", "Loc6")
dt.Rows.Add("XYZ7", "Loc6", "Loc9")
dt.Rows.Add("XYZ8", "Loc4", "Loc10")
dt.Rows.Add("XYZ9", "Loc6", "Loc13")
dt.Rows.Add("XYZ10", "Loc2", "Loc10")
'get a collection where A <> B
Dim diff = dt.AsEnumerable().Where(Function(x) x.Field(Of String)("Column_A") <> x.Field(Of String)("Column_B"))
'get a concat of A and B where A<>B and concat where A=B
Dim concat = diff.[Select](Function(x) x.Field(Of String)("Column_A")) _
.Concat(diff.[Select](Function(x) x.Field(Of String)("Column_B"))) _
.Concat(dt.AsEnumerable() _
.Where(Function(x) x.Field(Of String)("Column_A") = x.Field(Of String)("Column_B")) _
.[Select](Function(x) x.Field(Of String)("Column_A")))
'Group by to get the counts
Dim result = concat.GroupBy(Function(value) value).[Select](Function(group) New With { _
Key .Value = group.Key, _
Key .Count = group.Count() _
})
Console.WriteLine("Value | Count")
For Each x In result
Console.WriteLine("{0} | {1}", x.Value, x.Count)
Next
结果
Value | Count
Loc1 | 1
Loc13 | 2
Loc6 | 5
Loc5 | 3
Loc4 | 2
Loc2 | 2
Loc10 | 3
Loc9 | 1