Wednesday, July 29, 2009

What is LINQ?

LINQ, or Language-Integrated Query, lets you query data using Visual Basic Language.

Some of the Visual Basic keywords for working with LINQ:

Keyword - From which identifies the source of data for the query, Where which provides a condition that specifies which elements are retrieved from the datasource, Order By which indicates how the elements that are returned by the query are sorted, Select which specifies the content of the returned elements, Group By which groups the elements by one or more data items and lets you perform aggregate functions on the groups, and Join which combines data from two data soures.

A LINQ query that retrieves key values from a sorted list

The employee sales sorted list

Dim employeeSales As New SortedList(Of String, Decimal)
employeeSales.Add("Gabriel", 1286.45D)
employeeSales.Add("de Jesus", 2433.49D)
employeeSales.Add("Quirit", 2893.85D)
employeeSales.Add("Velasco", 2094.53D)

A query expression that selects the employee names from the list

Dim employeeList = From sales In employeeSales_
Where sales.Value > 2000 _
Order By sales.Value Descending _
Select sales.Key

Code that excutes the query

Dim employeeDisplay As String = ""
For Each employee In employeeList
employeeDisplay &= employee & vbCrLf
Next
MessageBox.Show(employeeDisplay, "Sorted Employees With Sales Over $2000")

The output would be:

Quirit
de Jesus
Velasco

No comments:

Post a Comment