2013-11-25 285 views
0

对不起英文不好;LINQ如何处理空值

我的代码;

Dim otherDT As DataTable = retDS1.Tables(0)   
Dim dt As New DataTable 

dt.Columns.Add("COGRAFI_BOLGE_ADI") 
dt.Columns.Add("Count") 

Dim query = (From dr In (From d In otherDT.AsEnumerable Select New With {.COGRAFI_BOLGE_ADI = d("COGRAFI_BOLGE_ADI")}) Select dr.COGRAFI_BOLGE_ADI Distinct) 

For Each colName As String In query 
    Dim cName = colName 
    Dim cCount = (From row In otherDT.Rows Select row Where row("COGRAFI_BOLGE_ADI").ToString = cName).Count 
    dt.Rows.Add(colName, cCount) 
Next 

GridView1.DataSource = dt 
GridView1.DataBind() 

某些行包含空值; 如何处理空行?

输出:

Marmara 40 
Ege  10 
Akdeniz 2 
... 
+0

可能重复http://stackoverflow.com/questions/15580008/怎么办,我检查换使用,LINQ到SQL基本无效值 – DevelopmentIsMyPassion

回答

0

真不明白你问什么,但你在找这样的事情:

Dim otherDT As DataTable = retDS1.Tables(0)   
Dim dt As New DataTable 
dt.Columns.Add("COGRAFI_BOLGE_ADI") 
dt.Columns.Add("Count") 

Dim query = (From dr In (From d In otherDT.AsEnumerable 
         Select New With {.COGRAFI_BOLGE_ADI = d("COGRAFI_BOLGE_ADI")})   
      Select dr.COGRAFI_BOLGE_ADI Distinct) 

For Each colName As String In query 
    Dim cName As String = colName 
    Dim cCount As Integer = (From row In otherDT.Rows 
          Select row 
          Where row IsNot Nothing AndAlso 
           row("COGRAFI_BOLGE_ADI") IsNot Nothing AndAlso 
           row("COGRAFI_BOLGE_ADI").ToString = cName).Count 
    If cCount > 0 Then 
     dt.Rows.Add(colName, cCount) 
    End If 
Next 

GridView1.DataSource = dt 
GridView1.DataBind()