Saturday, June 16, 2012

query from one IEnumerable type collection to another

In reading C# 4.0 in a Nutshell, I have encountered ways to make one IEnumerable from another of a different type in the midst of a LINQ query. I will give an example that uses these three silly classes:

  1. namespace Whatever.Models
    {
       public class Contact
       {
          public string Name { get; set; }
          public string Phone { get; set; }
          public string Email { get; set; }
          public string Street { get; set; }
          public string City { get; set; }
       }
    }
       
  2. namespace Whatever.Models
    {
       public class Address
       {
          public string Street { get; set; }
          public string City { get; set; }
          public string State { get; set; }
       }
    }
       
  3. namespace Whatever.Models
    {
       public static class StateHelper
       {
          public static string GetStateForCity(string city)
          {
             switch (city)
             {
                case "Globe":
                   return "Arizona";
                case "West Palm Beach":
                   return "Florida";
                case "Santa Cruz":
                   return "California";
                default:
                   return "Texas";
             }
          }
       }
    }
       

Here is my example. This test passes.

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Whatever.Models;
using System.Linq;
namespace Whatever.Tests
{
   [TestClass]
   public class ContactTests
   {
      [TestMethod]
      public void QueryTest()
      {
         Contact[] contacts = new Contact[]
            {
               new Contact()
               {
                  Name = "Johnny",
                  Phone = "(512) 419-8788",
                  Email = "pil@example.com",
                  Street = "500 East Stassney",
                  City = "Austin"
               },
               new Contact()
               {
                  Name = "Sid",
                  Phone = "(928) 425-629",
                  Email = "fevercheese@gmail.com",
                  Street = "1 South Broad",
                  City = "Globe"
               },
               new Contact()
               {
                  Name = "Steve",
                  Phone = "(561) 790-0349",
                  Email = "metal@example.com",
                  Street = "5055 Club",
                  City = "West Palm Beach"
               },
               new Contact()
               {
                  Name = "Paul",
                  Phone = "(831) 475-8325",
                  Email = "cook@example.com",
                  Street = "1515 Ocean",
                  City = "Santa Cruz"
               }
            };
         IEnumerable<Address> addresses = contacts
            .Where(c => c.Email.EndsWith("example.com"))
            .OrderBy(c => c.Name)
            .Select(c => new Address
               {
                  Street = c.Street,
                  City = c.City,
                  State = StateHelper.GetStateForCity(c.City)
               });
         List<Address> list = addresses.ToList();
         Assert.AreEqual(list.Count, 3);
         Assert.AreEqual(list[0].Street, "500 East Stassney");
         Assert.AreEqual(list[0].City, "Austin");
         Assert.AreEqual(list[0].State, "Texas");
         Assert.AreEqual(list[1].Street, "1515 Ocean");
         Assert.AreEqual(list[1].City, "Santa Cruz");
         Assert.AreEqual(list[1].State, "California");
         Assert.AreEqual(list[2].Street, "5055 Club");
         Assert.AreEqual(list[2].City, "West Palm Beach");
         Assert.AreEqual(list[2].State, "Florida");
      }
   }
}

 
 

Here is the same thing with an anonymous type.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Whatever.Models;
using System.Linq;
namespace Whatever.Tests
{
   [TestClass]
   public class ContactTests
   {
      [TestMethod]
      public void QueryTest()
      {
         Contact[] contacts = new Contact[]
            {
               new Contact()
               {
                  Name = "Johnny",
                  Phone = "(512) 419-8788",
                  Email = "pil@example.com",
                  Street = "500 East Stassney",
                  City = "Austin"
               },
               new Contact()
               {
                  Name = "Sid",
                  Phone = "(928) 425-629",
                  Email = "fevercheese@gmail.com",
                  Street = "1 South Broad",
                  City = "Globe"
               },
               new Contact()
               {
                  Name = "Steve",
                  Phone = "(561) 790-0349",
                  Email = "metal@example.com",
                  Street = "5055 Club",
                  City = "West Palm Beach"
               },
               new Contact()
               {
                  Name = "Paul",
                  Phone = "(831) 475-8325",
                  Email = "cook@example.com",
                  Street = "1515 Ocean",
                  City = "Santa Cruz"
               }
            };
         var addresses = contacts
            .Where(c => c.Email.EndsWith("example.com"))
            .OrderBy(c => c.Name)
            .Select(c => new
               {
                  Street = c.Street,
                  City = c.City,
                  State = StateHelper.GetStateForCity(c.City)
               });
         Assert.AreEqual(addresses.Count(), 3);
         Assert.AreEqual(addresses.ElementAt(0).Street, "500 East Stassney");
         Assert.AreEqual(addresses.ElementAt(0).City, "Austin");
         Assert.AreEqual(addresses.ElementAt(0).State, "Texas");
         Assert.AreEqual(addresses.ElementAt(1).Street, "1515 Ocean");
         Assert.AreEqual(addresses.ElementAt(1).City, "Santa Cruz");
         Assert.AreEqual(addresses.ElementAt(1).State, "California");
         Assert.AreEqual(addresses.ElementAt(2).Street, "5055 Club");
         Assert.AreEqual(addresses.ElementAt(2).City, "West Palm Beach");
         Assert.AreEqual(addresses.ElementAt(2).State, "Florida");
      }
   }
}

No comments:

Post a Comment