Saturday, August 13, 2011

Write a LINQ query for a dataset

Public Class Test
Shared Sub main()
'Data set to store the value
Dim dset As New DataSet()

'Data table
Dim dt As New DataTable

'Adding columns to data table
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Address", GetType(String))

'Adding rows to the data table
dt.Rows.Add("Asha", "1", "Tumkur")
dt.Rows.Add("Usha", "2", "Bangalore")
dt.Rows.Add("Ashok", "3", "")
dt.Rows.Add("", "4", "Mysore")
dt.Rows.Add("", "5", "")

'Adding data table to the Data set
dset.Tables.Add(dt)


'LINQ to get required data
Dim DataWithName = From detail In dset.Tables(0) Where detail.Item("Name") IsNot String.Empty Select detail
Dim DataWithOutName = From detail In dset.Tables(0) Where detail.Item("Name") Is String.Empty Select detail

Dim DataWithAddress = From detail In dset.Tables(0) Where detail.Item("Address") IsNot String.Empty Select detail
Dim DataWithotAddress = From detail In dset.Tables(0) Where detail.Item("Address") Is String.Empty Select detail

'Displying the result
For Each rows As DataRow In DataWithName
Console.WriteLine(rows.Item("Name"))
Next

Console.ReadLine()
End Sub

End Class

No comments:

Post a Comment