LINQ如何处理空值

时间:2013-11-25 09:54:32

标签: vb.net linq linq-to-sql

抱歉英语不好;

我的代码;

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

1 个答案:

答案 0 :(得分: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()