Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Tuesday, May 12, 2009

Weird things with LINQ - stripping unwanted unicodes.

So here was my problem, I needed to strip any unicode characters above 255.
At first I thought of using a regular expression to do a replace with but then the exclusion clause might be a bit long. Then I thought "can I do it with LINQ?".

Here is the result:

  private static string StripUniCode(string originalText)

        {

            var chars = originalText.ToCharArray();

            var result = from chr in chars

                        where chr <= 255

                        select chr;

            StringBuilder sb = new StringBuilder();

            sb.Append(result.ToArray<char>());

 

            return sb.ToString().Trim();

 

        }


Not brilliant but what the hell. You gotta love LINQ.
 

    

Sunday, April 20, 2008

Tools for exploring Linq

One year down and one year to go on my masters. I have month without study ahead of me so time to catch up on some of the new features in .Net 3.5. To this end I have decided to tackle Linq in all, at least of, its permutations.

Rather than spinning up an instance of VS2008 just to explore some simple examples and ideas I am a couple of tools to assist my explorations.

The first one is Snippet Compiler by Jeff Key. There has been a version of this around since .Net 1.1 and the latest version, Snippet Compiler Live 2008 Ultimate Edition for Developers (Alpha), is a real gem. This is a great little tool to have for testing bits of code or exploring what a class does. At the moment I have been using to play with the code examples in "Introducing Linq" which is a free e-book that can be found at http://introducinglinq.com/. The only gotcha you need to keep in mind is you need to reference the relevant libraries. For the underlying Linq stuff this System.Core.

The other tool that I have just come across this weekend is LinqPad by Joseph Albahari. This application has been written as tool to explore Linq and has some nice database features. I haven't had a real chance to play with it yet but it is one that I will be recommending to colleagues.

So there you go, if you are looking for a light-weight utility to explore Linq with either of these will do the job.

So it is a tip of the hat to Jeff and Joseph.