如何在c#中将列名称作为另一个值的值 [英] How to get column name as a value with another value in c#
问题描述
数据库表
----------------------------- ------------------
Id |日期| Test1 | Test2 | Test3
----------------------------------------- ------
1 | 01/02/2015 | xyz | pqr | abc
1 | 02/02/2015 | xyz1 | pqr1 | abc1
1 | 03/02/2015 | xyz2 | pqr2 | abc2
1 | 04/02/2015 | xyz3 | pqr3 | abc3
----------------------------------------- ------
怎么显示这样的?
----------------------------------------------- --------------
ColumnName | FirstDateValue | LastDateValue
----------------------------------------- --------------------
Test1 | xyz | xyz3
Test2 | pqr | pqr3
Test3 | abc | abc3
----------------------------------------- --------------------
请帮帮我
感谢和问候
Manmohan Jha
Database Table
-----------------------------------------------
Id | Date | Test1 | Test2 | Test3
-----------------------------------------------
1 | 01/02/2015 | xyz | pqr | abc
1 | 02/02/2015 | xyz1 | pqr1 | abc1
1 | 03/02/2015 | xyz2 | pqr2 | abc2
1 | 04/02/2015 | xyz3 | pqr3 | abc3
-----------------------------------------------
How to show like this??
-------------------------------------------------------------
ColumnName | FirstDateValue | LastDateValue
-------------------------------------------------------------
Test1 | xyz | xyz3
Test2 | pqr | pqr3
Test3 | abc | abc3
-------------------------------------------------------------
Help me please
Thanks & Regards
Manmohan Jha
推荐答案
如果您使用SQL执行此操作,则可以使用UNPIVOT
。请参阅使用PIVOT和UNPIVOT [ ^ ]
您也可以使用UNION ALL
结构,例如
If you're doing this with SQL you can useUNPIVOT
. See Using PIVOT and UNPIVOT[^]
Also you can use aUNION ALL
structure, like
SELECT 'Test1', MIN(Test1), MAX(Test1) FROM TableName GROUP BY 'Test1'
UNION ALL
SELECT 'Test2', MIN(Test2), MAX(Test2) FROM TableName GROUP BY 'Test2'
UNION ALL...
如果这纯粹是C#和数据表,那么只需循环遍历行并创建一个新的数据表。
If this is purely C# and a datatable then just loop through the rows and create a new datatable.
这篇关于如何在c#中将列名称作为另一个值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!