在平时的工作中,可能有很多情况下要对DataTable的里面的数据进行处理,最简单的例子如:对DataTable中的某个字段进行排序,根据条件筛选DataTable中的数据。。。,例子很多,下面我们看看如何处理的:
这是我最初写的代码:

{
DataTable dt = getDatatable();
try
{
var query = from q in dt.AsEnumerable()
where q.Field < string > ( " IPLocation " ) == " 北京 "
select new
{
IPid = q.Field < int > ( " IPid " ),
IPFrom = q.Field < string > ( " IPFrom " ),
IPTo = q.Field < string > ( " IPTo " ),
IPCity = q.Field < string > ( " IPCity " ),
IPToNumber = q.Field < string > ( " IPToNumber " ),
IPFromNumber = q.Field < string > ( " IPFromNumber " )
};
return query;
}
catch
{
return null ;
}
}
然后我在最后做数据源绑定的时候,总是报System.InvalidCastException: 指定的转换无效的错误,无论用Gridview的自动生成列,还是Reapter的手动添加列,都有问题,最后我查出生成的结果集就含有异常的存在,也就说再取结果集的时候,要慎重用如下写法:
IPid = q.Field<int>("IPid"),
IPFrom = q.Field<string>("IPFrom"),
IPTo = q.Field<string>("IPTo"),
IPCity = q.Field<string>("IPCity"),
IPToNumber = q.Field<string>("IPToNumber"),
IPFromNumber = q.Field<string>("IPFromNumber")
我后来尝试了另一种写法:
IPid = q["IPid"].ToString(),
IPFrom = q["IPFrom"].ToString(),
IPTo = q["IPTo"].ToString(),
IPLocation = q["IPLocation"].ToString(),
IPCity = q["IPCity"].ToString(),
IPToNumber = q["IPToNumber"].ToString(),
IPFromNumber = q["IPFromNumber"].ToString()
一开始我调试也还是报System.InvalidCastException: 指定的转换无效这个错误,我调试了一段时间,也还是有问题,我在想是不是数据绑定控件的问题,后来我将原来的Gridview自动生成列换成了手动输入列:

< asp:Repeater ID = " rpTest " runat = " server " >
< HeaderTemplate >
< tr >
< th style = " background-color:Gray; color:White;border: Gray 1px solid;text-align:center " > ID </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > IPFrom </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > IPTo </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > IPLocation </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > City </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > IPToNumber </ th >
< th style = " background-color:Black; color:White;border: Gray 1px solid;text-align:center " > IPFromNumber </ th >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPid " ) %></ td >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPFrom " ) %></ td >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPTo " ) %></ td >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPLocation " ) %></ td >
< td style = " border: Gray 1px solid;text-align:center; " >
<% #Eval( " IPCity " ) %>
</ td >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPToNumber " ) %></ td >
< td style = " border: Gray 1px solid;text-align:center; " ><% #Eval( " IPFromNumber " ) %></ td >
</ tr >
</ ItemTemplate >
</ asp:Repeater >
</ table >
后来问题就解决了,我现在也不知道这个问题什么原因,为什么不支持自动产生列呢(等待高手帮忙解决)?
知道问题的所在了,然后就可以随心的处理DataTable里面的数据了,如下面一个简单的小例子:

{
DataTable dt = getDatatable();
try
{
var query = from q in dt.AsEnumerable()
orderby long .Parse(q[ " IPid " ].ToString()) descending
where q[ " IPid " ].ToString() == " 345058 "
select new
{
IPid = q[ " IPid " ].ToString(),
IPFrom = q[ " IPFrom " ].ToString(),
IPTo = q[ " IPTo " ].ToString(),
IPLocation = q[ " IPLocation " ].ToString(),
IPCity = q[ " IPCity " ].ToString(),
IPToNumber = q[ " IPToNumber " ].ToString(),
IPFromNumber = q[ " IPFromNumber " ].ToString()
};
return query;
// List<IPInfo> list = new List<IPInfo>();
// foreach (var q in query)
// {
// IPInfo model = new IPInfo();
// model.IPCity = q.IPCity;
// model.IPFrom = q.IPFrom;
// model.IPFromNumber = q.IPFromNumber;
// model.IPid = long.Parse(q.IPid);
// model.IPLocation = q.IPLocation;
// model.IPTo = q.IPTo;
// model.IPToNumber = q.IPToNumber;
// list.Add(model);
// }
// return list;
}
catch
{
return null ;
}
}
当然更复杂的对DataTable的操作这里就不在敖述了,在这里我只是说明一下困扰我的问题。