Philly.Net recently had a very successful event called CSharpenUp. We spent the day talking about some advanced topics for C# developers. We had 4 speakers and 80+ attendees. The feedback was very positive, everyone learned a lot and had fun too. If we can set it up, we’ll do the same event in the fall for those that missed it.

I’m still gathering code samples from the other presenters. But for now, here is mine:

Code samples and slides for my LINQ session: Intro to LINQ

Code samples and slides for my Unit Testing session: Unit Testing Made Easy

Also, FYI we did try to record the sessions using Camtasia. Unfortunately, the mic we used wasn’t that great. I’m trying to salvage mine but I may just re-record them.

Linq is awesome and always makes tasks easier!  Here is a great tip that you might not know about.

Today I needed to compare two generic lists… SequenceEqual() to the rescue.  To show how it works, here is some sample code:

[Test]
public void SuccessfullyMatchesTwoListsOfInts()
{
    List<int> ints1 = new List<int>();
    ints1.Add(1);
    ints1.Add(2);
    ints1.Add(3);

    List<int> ints2 = new List<int>();
    ints2.Add(1);
    ints2.Add(2);
    ints2.Add(3);

    Assert.That(ints1.SequenceEqual(ints2), Is.True);
}

SequenceEqual loops through the 2 lists and compares each item to make sure they match.  Just for kicks I put it through the paces.  Here are some other unit tests that I wrote.  This helps show what you can do:

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace SequenceEqual
{
    [TestFixture]
    public class SequenceEqualTests
    {
        [Test]
        public void SuccessfullyMatchesTwoListsOfInts()
        {
            List<int> ints1 = new List<int>();
            ints1.Add(1);
            ints1.Add(2);
            ints1.Add(3);

            List<int> ints2 = new List<int>();
            ints2.Add(1);
            ints2.Add(2);
            ints2.Add(3);

            Assert.That(ints1.SequenceEqual(ints2), Is.True);
        }

        [Test]
        public void SuccessfullyFailsTwoListsOfInts()
        {
            // these two don't match!
            List<int> ints1 = new List<int>();
            ints1.Add(1);
            ints1.Add(2);
            ints1.Add(3);

            List<int> ints2 = new List<int>();
            ints2.Add(1);
            ints2.Add(4);
            ints2.Add(3);

            Assert.That(ints1.SequenceEqual(ints2), Is.False);
        }

        [Test]
        public void SuccessfullyFailsTwoListsOfIntsOutOfOrder()
        {
            // these two don't match. They have the same contents but the order is changed.
            List<int> ints1 = new List<int>();
            ints1.Add(3);
            ints1.Add(2);
            ints1.Add(1);

            List<int> ints2 = new List<int>();
            ints2.Add(1);
            ints2.Add(2);
            ints2.Add(3);

            Assert.That(ints1.SequenceEqual(ints2), Is.False);
        }

        [Test]
        public void SuccessfullyFailsTwoListsOfStrings()
        {
            // these two lists of strings don't match
            List<string> strings1 = new List<string>();
            strings1.Add("one");
            strings1.Add("two");
            strings1.Add("three");

            List<string> strings2 = new List<string>();
            strings2.Add("one");
            strings2.Add("lasjdflkajsdf");
            strings2.Add("three");

            Assert.That(strings1.SequenceEqual(strings2), Is.False);
        }

        [Test]
        public void SuccessfullyMatchesTwoListsOfStrings()
        {
            // two matching lists of strings.
            List<string> strings1 = new List<string>();
            strings1.Add("one");
            strings1.Add("two");
            strings1.Add("three");

            List<string> strings2 = new List<string>();
            strings2.Add("one");
            strings2.Add("two");
            strings2.Add("three");

            Assert.That(strings1.SequenceEqual(strings2), Is.True);
        }

        [Test]
        public void SuccessfullyMatchesTwoListsOfObjects()
        {
            // works for objects too.
            List<TestObject> objects1 = new List<TestObject>();
            objects1.Add(new TestObject { Property1 = "1" });
            objects1.Add(new TestObject { Property1 = "2" });
            objects1.Add(new TestObject { Property1 = "3" });

            Assert.That(objects1.SequenceEqual(objects1), Is.True);
        }

        [Test]
        public void SuccessfullyFailsTwoListsOfObjects()
        {
            // these two lists of objects may "look" the same, but they aren't
            // the same object (reference equals!)
            List<TestObject> objects1 = new List<TestObject>();
            objects1.Add(new TestObject { Property1 = "1" });
            objects1.Add(new TestObject { Property1 = "2" });
            objects1.Add(new TestObject { Property1 = "3" });

            List<TestObject> objects2 = new List<TestObject>();
            objects2.Add(new TestObject { Property1 = "1" });
            objects2.Add(new TestObject { Property1 = "2" });
            objects2.Add(new TestObject { Property1 = "3" });

            Assert.That(objects1.SequenceEqual(objects2), Is.False);
        }

        [Test]
        public void SuccessfullyMatchesTwoListsOfObjectsWithCustomComparer()
        {
            // here I use a custom comparer to see if the 
            // two lists of objects have the same content.
            List<TestObject> objects1 = new List<TestObject>();
            objects1.Add(new TestObject { Property1 = "1" });
            objects1.Add(new TestObject { Property1 = "2" });
            objects1.Add(new TestObject { Property1 = "3" });

            List<TestObject> objects2 = new List<TestObject>();
            objects2.Add(new TestObject { Property1 = "1" });
            objects2.Add(new TestObject { Property1 = "2" });
            objects2.Add(new TestObject { Property1 = "3" });

            Assert.That(objects1.SequenceEqual(objects2, new TestObjectComparer()), Is.True);
        }

        [Test]
        public void SuccessfullyFailsTwoListsOfObjectsWithCustomComparer()
        {
            // making sure the custome comparer fails when they don't match
            List<TestObject> objects1 = new List<TestObject>();
            objects1.Add(new TestObject { Property1 = "1" });
            objects1.Add(new TestObject { Property1 = "2" });
            objects1.Add(new TestObject { Property1 = "3" });

            List<TestObject> objects2 = new List<TestObject>();
            objects2.Add(new TestObject { Property1 = "1" });
            objects2.Add(new TestObject { Property1 = "asdfsadf" });
            objects2.Add(new TestObject { Property1 = "3" });

            Assert.That(objects1.SequenceEqual(objects2, new TestObjectComparer()), Is.False);
        }
    }

    public class TestObject
    {
        public string Property1 { get; set; }
    }

    public class TestObjectComparer : IEqualityComparer<TestObject>
    {
        public bool Equals(TestObject x, TestObject y)
        {
            return (x.Property1 == y.Property1);
        }

        public int GetHashCode(TestObject obj)
        {
            return obj.GetHashCode();
        }
    }
}

Today I needed to compare to generic lists. I guessed that Linq would provide a way to do this and sure enough it did: SequenceEqual() to the rescue. To show how it work

You’ve heard about LINQ to SQL, LINQ to Objects and LINQ to XML right?  Well how about LINQ to String?  Well technically there is no such thing.  But you use LINQ on a string!  You may not have realized this before but it works.  LINQ can be used to query IEnumerable<T> and a string is actually a collection of chars.  So yup, you can actually query the string itself.  Here are a few simple examples but you certainly do a lot more than this…

public void LinqToStringSample()
{
    string letters = "keuiwierqewefqwfwefcjlkjkl";

    //count all 'e'
    int count = letters.Count(l => l == 'e');

    //order
    var ordered = letters.OrderBy(l => l);
    
}

Tomorrow I’m presenting “Introduction to LINQ” at the Microsoft Event: Data Access Firestarter.  I’m trying to be proactive and actually post my samples and slides in advance!  This way, they’ll be there if anyone wants to take a look.  I’ll be doing the same demo at Philly.Net Code Camp on April 10 too.

Click here to download the samples: Demo and Slides 

If you attended the Firestarter, I hope you enjoyed my talk.  If not, I hope to see you at Code Camp.  And if you just happened along and are interested in learning LINQ, enjoy the samples.  Email me if you have any questions.

I’m pretty excited about two upcoming events coming to our community and I have an active role in both of them. 

First, I’ll be presenting at the .Net Data Access Firestarter on March 17, 2010.  This event, hosted at the Microsoft Office in Malvern, PA covers a variety of .Net data access strategies including LINQ to SQL, WCF Data Services and OData, Entity Framework, and even Azure Data Storage.  It’s a pretty good variety of information.  I’ll be doing one of the earliest sessions on the day, my topic is “Introduction To LINQ”.  I’m excited about this because LINQ is such an important part of the .Net Framework now.  While it isn’t really a data access technology, most of the data access technologies use LINQ!  It should be a lot of fun and I am honored to be a part of the event.  Also, if you can’t make it out to the event, you can watch it online too!

Here is a link to more information.

Second is of course, Philly.Net Code Camp.  Our next Code Camp is April 10, 2010.  Once again we are back at DeVry University.  These Code Camps just continue to get better and better.  Once again, we 60 sessions in a variety of technologies.  In addition, we are broadcasting a portion of the content via Live Meeting and they’ll also be available for download later.  That alone makes this an exciting event.  This year, we’ve also invited some folks from Alex’s Lemonade Stand to come by.  At the end of the day, we’ll be presenting them with a donation to their worthwhile organization.  How are we raising the money?  This year we are offering two kinds of tickets to Code Camp.  The first is the standard, free ticket.  Anyone is welcome to come to Code Camp for free and enjoy the day (and breakfast and lunch are included as always).  But we have added a Booster ticket this year for $25.  If attendees choose to donate this small amount, they get a few benefits:

  • $5 of the each Booster ticket goes to Alex’s Lemonade Stand
  • Boosters are included in some premium raffles
  • Boosters are included in the post Code Camp party
  • Booster money will help sustain Philly.Net throughout the year.
  • Other benefits are included as well.

This is sort of an experiment.  The leadership, of which I am a member, doesn’t know how this will turn out.  It seems to me that $25 is a small price to pay for a ton of content at Code Camp, not to mention the fact that we provide breakfast and lunch.  And we hope that people won’t mind donating to our efforts, it takes a lot of money to run the organization and all of the events year round.  Plus, we are donating a portion to charity.  On the other hand, I always liked these events being free.  People donate their time so that others can come and learn for free.  It’s a great concept!  But times are changing and we need money to run our organization year round.

In any case, whether you choose to donate or attend for free, I hope to see you at Code Camp.