Tuesday, July 1, 2014

using .Contains versus using .Any in C#

Given this class...

using System.Collections.Generic;
namespace ContainsExample.Models
{
   public class Planet
   {
      public string Name { get; set; }
      public List<string> Moons { get; set; }
   }
}

 
 

...this test will pass:

using System.Collections.Generic;
using System.Linq;
using ContainsExample.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ContainsExample.Tests
{
   [TestClass]
   public class ContainsTests
   {
      [TestMethod]
      public void TestAgainstPlanets()
      {
         List<Planet> planets = new List<Planet>()
         {
            new Planet()
            {
               Name = "Mercury",
               Moons = new List<string>(){}
            },
            new Planet()
            {
               Name = "Venus",
               Moons = new List<string>(){}
            },
            new Planet()
            {
               Name = "Earth",
               Moons = new List<string>(){ "Luna" }
            },
            new Planet()
            {
               Name = "Mars",
               Moons = new List<string>(){ "Phobos", "Deimos" }
            }
         };
         Planet planet = planets.Where(p => p.Moons.Contains("Deimos")).Single();
         Assert.AreEqual(planet.Name, "Mars");
      }
   }
}

 
 

However if we change the original class like so...

using System.Collections.Generic;
namespace ContainsExample.Models
{
   public class Planet
   {
      public string Name { get; set; }
      public List<Moon> Moons { get; set; }
   }
}

 
 

...and bring into the mix a second, new class like this...

namespace ContainsExample.Models
{
   public class Moon
   {
      public string Name { get; set; }
   }
}

 
 

...then we will need to use .Any instead of .Contains as seen here:

using System.Collections.Generic;
using System.Linq;
using ContainsExample.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ContainsExample.Tests
{
   [TestClass]
   public class ContainsTests
   {
      [TestMethod]
      public void TestAgainstPlanets()
      {
         List<Planet> planets = new List<Planet>()
         {
            new Planet()
            {
               Name = "Mercury",
               Moons = new List<Moon>(){}
            },
            new Planet()
            {
               Name = "Venus",
               Moons = new List<Moon>(){}
            },
            new Planet()
            {
               Name = "Earth",
               Moons = new List<Moon>(){ new Moon() { Name = "Luna" } }
            },
            new Planet()
            {
               Name = "Mars",
               Moons = new List<Moon>(){ new Moon() { Name = "Phobos" }, new Moon() {
                     Name = "Deimos" } }
            }
         };
         Planet planet = planets.Where(p => p.Moons.Any(m => m.Name
               == "Deimos")).Single();
         Assert.AreEqual(planet.Name, "Mars");
      }
   }
}

 
 

This second test passes as well!

No comments:

Post a Comment