article

Linq Examples

Email
Submitted on: 4/15/2016 10:05:40 AM
By: Salem Al Shekaili 
Level: Intermediate
User Rating: Unrated
Compatibility: VB.NET
Views: 2619
 
     Four examples for LINQ

 
				Imports System
Imports System.Collections.Generic
Imports System.Linq
Module Module1
Sub Main()
'Linq Examples
'Select names from an array that contain 'a' letter then order them by lenght and then converts the results to uppercase.
Dim names() As String = {"Tom", "Dick", "Harry", "Mary", "Jay"}
Dim query As IEnumerable(Of String) = names.Where(Function(n) n.Contains("a")).OrderBy(Function(n) n.Length).Select(Function(n) n.ToUpper())
For Each name As String In query
Console.Write(name + "|")
Next
'output: JAY|MARY|HARRY|
' the following code will give similar results of the preceding one
Dim names3() As String = {"Tom", "Dick", "Harry", "Mary", "Jay"}
Dim query3 As IEnumerable(Of String) = From n In names3
Where n.Contains("a")
Order By n.Length
Select n.ToUpper() ' Translate each element (project)
Console.Write(Environment.NewLine)
Console.Write(Environment.NewLine)
For Each name As String In query3
Console.Write(name + "|")
Next
'Get names2's lenght
Dim query2 As IEnumerable(Of Integer) = names.Select(Function(n) n.Length)
Console.Write(Environment.NewLine)
Console.Write(Environment.NewLine)
For Each length As Integer In query2
Console.Write(length & "|")
Next
'output: 5|7|5|4|3|
'Here we wanted to remove all vowels from a string
Console.Write(Environment.NewLine)
Console.Write(Environment.NewLine)
Dim query4 As IEnumerable(Of Char) = "Not what you might expect"
query4 = query4.Where(Function(c) c <> "a"c)
query4 = query4.Where(Function(c) c <> "e"c)
query4 = query4.Where(Function(c) c <> "i"c)
query4 = query4.Where(Function(c) c <> "o"c)
query4 = query4.Where(Function(c) c <> "u"c)
For Each c As Char In query4 ' Nt wht y mght xpct
Console.Write(c)
Next
Console.ReadLine()
End Sub
End Module


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.