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> 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;