Friday, July 13, 2012

using let in LINQ query syntax

The following test passes and shows off something else I learned from C# 4.0 in a Nutshell.

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace LetExample.Tests
{
   [TestClass]
   public class LetTest
   {
      [TestMethod]
      public void TestLet()
      {
         int[] digits = {1, 2, 3, 4, 5, 6, 7};
         IEnumerable<int> query = from d in digits
               let splits = (d%2)
               where splits.Equals(0)
               select d;
         Assert.AreEqual(query.Count(), 3);
         Assert.AreEqual(query.ElementAt(0), 2);
         Assert.AreEqual(query.ElementAt(1), 4);
         Assert.AreEqual(query.ElementAt(2), 6);
      }
   }
}

No comments:

Post a Comment