logo

C# How to get the First item in a linq result set

Overview: This codesnippet orders a collection of products by price then selects the first product in the list.
 public class UnitTest1
    {
        private Product[] products =
        {
        new Product {Name="Kayak",Category="Watersports",Price=275m},
        new Product {Name="Lifejacket", Category="Watersports",Price=48.95m},
        new Product {Name="Soccer Ball", Category="Soccer",Price=19.50m},
        new Product {Name="Corner Flag", Category="Soccer",Price=34.95m}
         };
  [TestMethod]
        public void TestLinqFirst()
        {
            var query = products.OrderByDescending(c => c.Price)
                .Select(c => c)
                .First();

            Assert.AreEqual(query.Name, "Kayak");
        }
}
s