2013-11-25 461 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()