A Little Bit of LINQ


The following was posted as a response to a question on Stack Overflow, which is an interesting site I frequent.

Stack Overflow is a community web site of programmers helping programmers. It is a site which allows programmers to post questions, and other programmers post answers. It may sound like a site which would not work because: “Why would you do it for free?”.

The answer to that question is probably a manifold response, or many faceted. Some of those elements would include:

  • Altruism. Programming is an altruistic professions. As a programmer you develop “stuff” for people you may,or may not, know, to help them do something. 
  • Self Improvement. Programming is a profession in which one needs to be learning all the time. There are always new technologies, or technologies which you’ve not worked in before.
  • Didactic. For programmers teaching becomes another “string to the bow” in the profession. There is always something which needs to be explained, or taught, for developer.
  • Self Interest. This is a bit of a follow on from the didactic point, which could be summarised as “If I improve the quality of programmers in general, then there may be one less mess I have to clean up, or a piece of software which has fewer bugs in it”.

The Question

In LINQ, can I select multiple items?

In Summary

Given a sequence like:

string [] foos = { "abc", "def", "ghi" };

Produce a collection which looks like:

string[] result = {"abc", "cba", "def", "fed", "ghi", "ihg"};

The Answer (my answer at least)

static void Main(string[] args)
{
string[] foos = { "abc", "def", "ghi" };
// Just to test how to reverse strings
string[] reveresed = (from strings in foos
select new string(strings.ToCharArray().Reverse().ToArray())).ToArray();
// the solution
string[] result = foos.Union(foos.Select(A=> new string(A.ToCharArray().Reverse().ToArray()))).ToArray()
// output the result to the debug console
foreach(string a in result) Debug.WriteLine(a)
}

Key Points:

  • The extension method Union is used to concatenate the original sequence with the reversed sequence.
  • The strings are reversed by converting them to a char[] (using the string ToCharArray method) and then using the Reverse extension method to reverse that array’s order.
  • The new strings for the result sequence are created by calling the constructor of the string object which accepts a char[] (new string(char[])).
  • The results are dumped to the debug console (just to check we got what we were after).
Advertisements

, , , , , , , , ,

  1. Yield Return: Uses, Abuses and “Rules of Thumb” « Craig's Eclectic Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: