August 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Range Variables and Let Statements in LINQ

  • February 29, 2008
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Having defined the range words, you can use these words within the same query. In the example, it is the words you want, not the whole substring. The result of the query is a sequence containing the individual words only.

Listing 3: Using the Let clause to define a temporary range that the query can reuse.

Sub LetRange()
   Dim markTwain = New String() _
      {"Clothes make the man. Naked people", _
       "have little or no influence in society."}


   Dim noInfluence = From twain In markTwain _
      Let words = twain.Split(New Char() {".", " "}, _
         StringSplitOptions.RemoveEmptyEntries) _
      From word In words _
      Select word

   For Each w In noInfluence
      Console.WriteLine(w)
   Next
   Console.ReadLine()

End Sub

Listing 4 shows how you can reuse the temporary range variable multiple times in tehe same query. In this revision, all of the items in words are converted to all uppercase letters.

Listing 4: Using the temproary range variable multiple times in the same query.

Dim noInfluence = From twain In markTwain _
   Let words = twain.Split(New Char() {".", " "}, _
      StringSplitOptions.RemoveEmptyEntries) _
   From word In words _
   Let upper = word.ToUpper() _
   Select upper

Summary

Just know that LINQ is a masterful work of software engineering, and I hope that understanding ranges a little better will help you make better use of this cool new technology.

Biography

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book, LINQ Unleashed for C#, due in Spring 2008. You may contact him for technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Glugnet opened a users group branch in Flint, Michigan in August 2007. If you are interested in attending, check out the www.glugnet.org web site for updates.

Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel