现有格式:
Name    year    value
A       2009    1
A       2010     2
A       2011     3
B       2009     11
B       2010     22
B       2010     33 转化为
Name  2009    2010    2011
A     1       2        3
B     11      22       33 
怎么做?请教高手了。 可以用Linq 也可以用代码。最好是用LInq。另外主要问题是 2009 2010 2011 是不定的。 是会变化的。所以不能写死。
所以要做成动态的。

解决方案 »

  1.   

    http://topic.csdn.net/u/20110620/15/bec4adee-ebd6-40c7-88f6-250050f79680.html看这个帖子中我的回复。
      

  2.   

    SQL Server中实现
    http://blog.csdn.net/taomanman/article/details/6684013
      

  3.   

    http://topic.csdn.net/u/20110318/14/5f7f3f4e-05a8-41f1-9116-a5813a758af2.html
      

  4.   

    加一句对Name列的排序 该加到哪里咯? 一楼高手。数据已经出来了。
      

  5.   

    C# 实现DataTable的行转列 .http://blog.csdn.net/sandy945/article/details/5336560DataList 实现 行转列 .http://blog.csdn.net/sandy945/article/details/4068760
      

  6.   

    OK!非常感谢各位高手,问题已经解决。没想到是要两步走才行。另外排序也加上了。不明白为什么排序不能加在select new 后面。 加它前面就ok了。特别感谢1楼。。
      

  7.   

    sql可以不?
    create table #temp(Name varchar(20),year varchar(20),value varchar(20))
    insert into #temp
    select 'A','2009','1' union all
    select 'A','2010','2' union all
    select 'A','2011','3' union all
    select 'B','2009','11' union all
    select 'B','2010','22' union all
    select 'B','2011','33'
    declare @sql varchar(500)
    set @sql = 'select Name'
    select @sql=@sql+',max(case year when '''+[year]+''' then value end) ['+[year]+']'
    from (select distinct [year] from #temp) as a
    set @sql = @sql +' from #temp group by Name'
    exec(@sql)
      

  8.   

    var result = result.OrderBy(x => x["Name"].ToString());