【发布时间】:2011-11-29 16:20:33
【问题描述】:
我有一个如下所示的 DataTable。我想知道是否有人知道使用 Linq 根据字符串长度规则的 DataTable 截断行值的一种很酷的方法。
数据表:
var testtable = new DataTable();
testtable.Columns.Add( "cola" , typeof( String ) );
testtable.Columns.Add( "colb" , typeof( String ) );
testtable.Columns.Add( "colc" , typeof( String ) );
testtable.Rows.Add( new object[] { "1" , "22" , "333" } );
testtable.Rows.Add( new object[] { "4444" , "55555" , "666666" } );
testtable.Rows.Add( new object[] { "7777777" , "88888888" , "999999999" } );
规则表:
var ruletable = new DataTable();
ruletable.Columns.Add( "columnname" , typeof( String ) );
ruletable.Columns.Add( "length" , typeof( Int32 ) );
ruletable.Rows.Add( new object[] { "colb" , 3 } );
ruletable.Rows.Add( new object[] { "colc" , 4 } );
现实生活中的数据表有 180 列和 1000 行。规则表有 180 行(每列一个),所以我希望大师可以评论我如何使用 Linq 做一些动态的事情,以根据规则表中的内容从 testtable 中获取最左边的字符。
谢谢。
【问题讨论】:
-
我正在玩 DataTable.AsEnumerable 和 SetField 但到目前为止还没有取得任何进展。