LINQ: The Power of Lambdas with List<T>

Category: .NET C# Programming Tags: ,
June 22, 2015

If you are unsure of the power of lambdas, look at this:

Say you have a class like:

public class RoomQuote
   public int RoomID { get; set; }
   public string RoomName { get; set; }
   public int Price { get; set; }

Say the list of RoomQuote is populated by calling an external function:

List<RoomQuote> roomQuotes = Prices.GetRoomQuotes( fromDate, numberNights );

To get the lowest and highest quote is a piece of cake:

int lowestQuote = roomQuotes.Min( x => x.Price );

int highestQuote = roomQuotes.Max( x => x.Price );