Sunday, January 27, 2008

A LINQ query to generate names for test data

I needed to generate a large test file of names to test a HR function but for privacy reasons did not want to use the actual names in the HR system. My solution was to grab a web page of names from Wikipedia, save it as a text file, and use LINQ to filter out just the names. Worked like a charm and the LINQ portion was essentially a single line of code. The VB.Net source code snippet is shown below:

Dim Lines As String() = System.IO.File.ReadAllLines(strFileName)
Dim names = From line In Lines _
Select line _
Where (line.Contains(","))
And (Not line.Contains(" to "))
And (Not line.Contains(" of "))
And (line.IndexOfAny(":$""()") = -1)
And (line.Length < 30)

Hope that helps.

Joe Kunk
Okemos, MI USA

No comments: