-
Aug 21st, 2017, 03:48 PM
#1
Thread Starter
Addicted Member
LINQ to get an Item from an array and Debug.WriteLine it.
Starting to use LINQ. Using this LINQ code to Filter in strings in an array that Contain the word Visual.
Code:
Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
Dim selectedWords = From word In words
Where word.ToUpper.Contains("VISUAL")
Select word
For Each word In words
Debug.WriteLine(word.ToString)
Next
The result is :
Visual Basic
Java
Visual Studio
Not the one I expected and looking for. With Array.Filter works ok but I what to learn how to do it with LINQ. The output should be:
Visual Basic
Visual Studio
What is wrong ?. Thank you.
-
Aug 21st, 2017, 04:00 PM
#2
Thread Starter
Addicted Member
Re: LINQ to get an Item from an array and Debug.WriteLine it.
Solved it.
Code:
Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
Dim selectedWords = From word In words
Where word.ToUpper.Contains("VISUAL")
Select word
For Each wrd In selectedWords
Debug.WriteLine(wrd.ToString)
Next
RESULT:
Visual Basic
Visual Studio
-
Aug 23rd, 2017, 08:24 AM
#3
Re: LINQ to get an Item from an array and Debug.WriteLine it.
For the record here is another method
Code:
Module Module1
Sub Main()
Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
Dim selectedWords = From item In
words Where item.IndexOf("visual", StringComparison.CurrentCultureIgnoreCase) > -1
For Each word As String In selectedWords
Console.WriteLine($"->{word}")
Next
Console.ReadLine()
End Sub
End Module
-
Aug 23rd, 2017, 08:34 AM
#4
Re: LINQ to get an Item from an array and Debug.WriteLine it.
well heck, as long as we're showing off here....
Code:
Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
Array.ForEach(words.Where(Function(w) w.Contains("Visual")).ToArray, Sub(w) Console.WriteLine(String.Format("-> {0}", w)))
-tg
-
Aug 26th, 2017, 07:07 AM
#5
Fanatic Member
Re: LINQ to get an Item from an array and Debug.WriteLine it.
Mark this thread as resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|