Dim employee
= From emp In db.emp_masters
_
Select emp.Empname
ListBox1.DataSource = employee
'DataGridView1.DataSource
= employee
End Sub
In next example, I am showing how to get the number of employess in each
department. For this I have added a DataGridView.
The code is as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db As New EmployeeDataContext
Dim employee
= From emp In db.emp_masters
_
Group emp By emp.EmpDept Into g
= Group _
Select New With {EmpDept,
.noofemp = g.Count()}
DataGridView1.DataSource = employee
End Sub
The result of the above code is as follows:
In our next example, I am showing a query with where clause.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db As New EmployeeDataContext
Dim employee
= From emp In db.emp_masters
_
Where emp.EmpAdd
= "New
Delhi" _
Select emp.Empname,
emp.EmpDesg, emp.EmpPhone
DataGridView1.DataSource = employee
End Sub
Output:
Also I take liberty to select the number of table columns I want to display
rather than whole. For displaying all columns write "Select emp" in the query
above.
Conclusion:
Here in this article we have seen how to query the database using LINQ. In later
articles we will see more of LINQ.