首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

LinqToDataTable ,DataTable中有NULL值数据时,报错误,如何解决

2012-12-15 
LinqToDataTable ,DataTable中有NULL值数据时,报异常,怎么解决?IEnumerableDataRow mQuery from q in

LinqToDataTable ,DataTable中有NULL值数据时,报异常,怎么解决?
IEnumerable<DataRow> mQuery = from q in pDt select q;
if (!string.IsNullOrEmpty(pCommunityName{
     mQuery = from q in mQuery
              where q.Field<string>("Community").Equals(pCommunityName)
              select q;
}
代码如上。
pDt是根据存储过程在数据库中取出数据转化为DataTable格式,
pDt中 Community列中NULL值,结果用linq的查询的时候,直接报异常。
请问有什么解决的办法。
[最优解释]

引用:
IEnumerable<DataRow> mQuery = from q in pDt select q;
if (!string.IsNullOrEmpty(pCommunityName{
     mQuery = from q in mQuery
              where q.Field<string>("Community").Equals(pCommunityName……

试试这个
IEnumerable<DataRow> rows = dt.AsEnumerable().Where(t => (t.Field<string>("Community") != null) && t.Field<string>("Community") == pCommunityName);
[其他解释]
q.Field<string>("Community").ToString().Equals(pCommunityName)

试试。。

[其他解释]
IEnumerable<DataRow> mQuery = from q in pDt select q;
=========
IEnumerable<DataRow> mQuery = from q in pDt where q.Field<string>("Community") != null select q;

热点排行