This article and the vb.net and c# downloads are to show how you can use lambda expressions as delegates to perform many functions on System.Collections.Generic.List(T), and in some cases any object that implements IEnumerable with more concise code.  Huh?  Typically I use Generic.List(T) to hold my object instances, so a lot of the methods I'm going to demonstrate lambda expressions with are exposed on the class Generic.List(T).  Some of these methods can be found in other generic lists also so feel free to use them there.  In addition, there is now an Enumerable Class in System.Linq.  This class holds extension methods that can be used on any object that implements IEnumerable(T).  List(T) happens to be one of those classes so in addition to it's own methods, we have access to these extensions methods.   What I want to focus on however, is not just the use of all these methods we now access to on List(T).  There are tons of code samples out there showing how to use these. 

 

What I would like to really focus on is one of the things that I have found the most useful in the .NET 3.0 release.  That is the use of lambda expressions when working with generic lists such as List(T).  Using lambda expressions with the different methods found in System.Linq.Enumerable (Extension Methods) and List(T) I am able to do things such as Sort, Filter, Order, Group, Format and perform aggregate functions on my List(T) collection of objects.  There's nothing that lambda expressions can do that couldn't be done before, it can just be done with less code now which I am personally a big fan of.  Also, it is nice for debugging purposes to keep simple function definitions located were they are used instead of sprawled out in your code.  Lambda expression originate form calculus, however by no means do you have to be a Fibonacci or a Pythagoras to figure them out.  For people like me, there's a little "understanding curve" but trust me, once you do about 10-15 of these they will start to click.

 

Let's get started.  Say we had a collection of stock objects in a generic list called stocks.  Stock has the properties StockID, CurrentPrice, PriceChange and instance of Industry.  How can we  output to a console window all stocks in the collection where the PriceChange was greater than $1.00.  There are several ways to accomplish this task.  The first three samples below show how we can accomplish this using a (1) Linq query, (2) delegate, and (3) lambda expression.  Please note that using the Linq query and the lambda expression both allow you to do the same thing with a single line of code in most cases.  However for me, especially with C# I prefer the conciseness of the lambda expressions.  After contrasting three different ways to accomplish the first task I will begin to demonstrate the methods I find most useful with some real world scenarios on how to use them.  These samples will all be done using lambda expressions and will not be contrasted with the first two approaches.

 

C# Sample Code


VB.Net Sample Code

C# Full Project Download

VB.Net Full Project Download

 

C# Code Sample

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4.   
  5. class Program   
  6. {   
  7.     /// <summary>   
  8.     /// Determines if stock's PriceChange is greater than zero.   
  9.     /// </summary>   
  10.     /// <param name="stock">Stock to check.</param>   
  11.     /// <returns>Boolean indicating if stock's PriceChange is greater than zero.</returns>   
  12.     private static bool GetDollarPlusGainers(Stock stock)   
  13.     {   
  14.         return stock.PriceChange > 1.00;   
  15.     }   
  16.   
  17.   
  18.   
  19.     /// <summary>   
  20.     /// Outputs formatted Stock.   
  21.     /// </summary>   
  22.     /// <param name="stock">Stock to format.</param>   
  23.     /// <param name="consoleColor">Color to format Stock with.</param>   
  24.     private static void OutputFormattedStockToDisplay(Stock stock, ConsoleColor? consoleColor)   
  25.     {   
  26.         Console.ForegroundColor = consoleColor.GetValueOrDefault(ConsoleColor.Yellow);   
  27.   
  28.         Console.WriteLine("StockID: " + stock.StockID);   
  29.         Console.WriteLine("Symbol: " + stock.Symbol);   
  30.         Console.WriteLine("Current Price: " + String.Format("{0:c}", stock.CurrentPrice));   
  31.         Console.WriteLine("Price Change: " + String.Format("{0:c}", stock.PriceChange));   
  32.         Console.WriteLine("IndustryID: " + stock.Industry.IndustryID);   
  33.         Console.WriteLine("Industry Name: " + stock.Industry.Name);   
  34.         Console.WriteLine("");   
  35.         Console.WriteLine("");   
  36.     }   
  37.   
  38.   
  39.   
  40.     static void Main(string[] args)   
  41.     {   
  42.         // get some stocks   
  43.         List<Stock> stocks = DataHelper.GetTestData();   
  44.   
  45.   
  46.         /***********************************************************************  
  47.          *  1. Get cStocks with PriceChange > 1.00 using linq query  
  48.         ***********************************************************************/  
  49.   
  50.         foreach (Stock stock in from s in stocks where s.PriceChange > 1.00 select s)   
  51.         {   
  52.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  53.         }   
  54.   
  55.         Console.ReadLine();   
  56.   
  57.   
  58.   
  59.         /***********************************************************************  
  60.          *  2. Get cStocks with PriceChange > 1.00 using delegate  
  61.          *   
  62.          *  Method(s): FindAll  
  63.         ***********************************************************************/  
  64.   
  65.         foreach (Stock stock in stocks.FindAll(GetDollarPlusGainers))   
  66.         {   
  67.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  68.         }   
  69.   
  70.         Console.ReadLine();   
  71.   
  72.   
  73.   
  74.         /***********************************************************************  
  75.          *  3. Get cStocks with PriceChange > 1.00 using Lambda Expression  
  76.          *   
  77.          *  Method(s): FindAll  
  78.         ***********************************************************************/  
  79.   
  80.         foreach (Stock stock in stocks.FindAll(s => s.PriceChange > 1.00))   
  81.         {   
  82.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  83.         }   
  84.   
  85.         Console.ReadLine();   
  86.   
  87.   
  88.         /***********************************************************************  
  89.          *  4. 'Count' Stock's in stocks > $100 and <=$100  
  90.          *   
  91.          *  Method(s): FindAll, Count  
  92.         ***********************************************************************/  
  93.   
  94.         Console.WriteLine("Total number of stocks: " + stocks.Count());   
  95.         Console.WriteLine("Number of stocks > $100: " + stocks.FindAll(s => s.CurrentPrice > 100).Count());   
  96.         Console.WriteLine("Number of stocks <= $100: " + stocks.FindAll(s => s.CurrentPrice <= 100).Count());   
  97.   
  98.         Console.ReadLine();   
  99.   
  100.   
  101.   
  102.         /***********************************************************************  
  103.          *  5. 'Max' and 'Min' by Stock.Price  
  104.          *   
  105.          *  Method(s): Max, Min  
  106.         ***********************************************************************/  
  107.   
  108.         Console.WriteLine("Current Max Stock Price: " + stocks.Max(s => Convert.ToDouble(s.CurrentPrice)));   
  109.         Console.WriteLine("Current Max Stock Price: " + stocks.Min(s => Convert.ToDouble(s.CurrentPrice)));   
  110.   
  111.         Console.ReadLine();   
  112.   
  113.   
  114.   
  115.         /***********************************************************************  
  116.          *  6. 'Average' CurrentPrice of all Stocks in stocks  
  117.          *   
  118.          *  Method(s): Average  
  119.         ***********************************************************************/  
  120.   
  121.         Console.WriteLine("Avg Current Stock Price: " + stocks.Average(s => Convert.ToDouble(s.CurrentPrice)));   
  122.   
  123.         Console.ReadLine();   
  124.   
  125.   
  126.   
  127.         /***********************************************************************  
  128.          *  7. 'OrderBy' Stock.Symbol  
  129.          *   
  130.          *  Method(s): OrderBy  
  131.         ***********************************************************************/  
  132.   
  133.         foreach (Stock stock in stocks.OrderBy(s => s.Symbol))   
  134.         {   
  135.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  136.         }   
  137.   
  138.         Console.ReadLine();   
  139.   
  140.   
  141.   
  142.         /***********************************************************************  
  143.          *  8. 'OrderBy' Stock.Industry.Name, 'ThenBy' Stock.Symbol  
  144.          *   
  145.          *   
  146.          *  Method(s): OrderBy, ThenBy  
  147.         ***********************************************************************/  
  148.   
  149.         foreach (Stock stock in stocks.OrderBy(s => s.Industry.Name).ThenBy(s => s.Symbol))   
  150.         {   
  151.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  152.         }   
  153.   
  154.         Console.ReadLine();   
  155.   
  156.   
  157.   
  158.         /***********************************************************************  
  159.          *  9. 'OrderByDescending' Stock.Symbol  
  160.          *   
  161.          *   
  162.          *  Method(s): OrderByDescending  
  163.         ***********************************************************************/  
  164.   
  165.         foreach (Stock stock in stocks.OrderByDescending(s => s.Symbol))   
  166.         {   
  167.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  168.         }   
  169.   
  170.         Console.ReadLine();   
  171.   
  172.   
  173.   
  174.         /***********************************************************************  
  175.          *  10. 'Sort' stocks by Symbol (ASC)  
  176.          *   
  177.          *   
  178.          *  Method(s): Sort, CompareTo  
  179.         ***********************************************************************/  
  180.   
  181.         stocks.Sort(delegate(Stock x, Stock y) { return x.Symbol.CompareTo(y.Symbol); });   
  182.   
  183.         foreach (Stock stock in stocks)   
  184.         {   
  185.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  186.         }   
  187.   
  188.         Console.ReadLine();   
  189.   
  190.   
  191.   
  192.         /***********************************************************************  
  193.          *  11. Does stocks 'Contain' any Stocks.CurrentPrice > $100, $150  
  194.          *   
  195.          *   
  196.          *  Method(s): Contains, Find  
  197.         ***********************************************************************/  
  198.   
  199.         Console.WriteLine("The stocks collection contains stocks > $100: " + stocks.Contains(stocks.Find(s => s.CurrentPrice > 100)));   
  200.         Console.WriteLine("The stocks collection contains stocks > $150: " + stocks.Contains(stocks.Find(s => s.CurrentPrice > 150)));   
  201.   
  202.         Console.ReadLine();   
  203.   
  204.   
  205.         /***********************************************************************  
  206.          *  12. List cStock's in Stocks.CurrentPrice > $50, $100  
  207.          *   
  208.          *   
  209.          *  Method(s): OrderByDescending, TakeWhile  
  210.         ***********************************************************************/  
  211.   
  212.         foreach (Stock stock in stocks.OrderByDescending(s => Math.Abs(s.CurrentPrice)).TakeWhile(s => s.CurrentPrice > 50))   
  213.         {   
  214.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  215.         }   
  216.   
  217.         Console.WriteLine("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-");   
  218.   
  219.         foreach (Stock stock in stocks.OrderByDescending(s => Math.Abs(s.CurrentPrice)).TakeWhile(s => s.CurrentPrice > 100))   
  220.         {   
  221.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  222.         }   
  223.   
  224.         Console.WriteLine("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-");   
  225.   
  226.         Console.ReadLine();   
  227.   
  228.   
  229.         /***********************************************************************  
  230.          *  13. 'Take' the top 3 stocks by Pricechange  
  231.          *   
  232.          *   
  233.          *  Method(s): OrderByDescending, Take  
  234.         ***********************************************************************/  
  235.   
  236.         foreach (Stock stock in stocks.OrderByDescending(s => Math.Abs(s.CurrentPrice)).Take(3))   
  237.         {   
  238.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  239.         }   
  240.   
  241.         Console.ReadLine();   
  242.   
  243.   
  244.   
  245.         /***********************************************************************  
  246.          *  14. Elements in stocks that are 'OfType' cStock (all in our case)  
  247.          *   
  248.          *   
  249.          *  Method(s): OfType  
  250.         ***********************************************************************/  
  251.   
  252.         foreach (Stock stock in stocks.OfType<Stock>())   
  253.         {   
  254.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  255.         }   
  256.   
  257.         Console.ReadLine();   
  258.   
  259.   
  260.   
  261.         /***********************************************************************  
  262.          *  15 Do 'All' cStock's in stocks have CurrentPrice > 10.00, .01  
  263.          *   
  264.          *   
  265.          *  Method(s): All  
  266.         ***********************************************************************/  
  267.   
  268.         Console.WriteLine("All stocks are > 10.00: " + stocks.All(s => s.CurrentPrice > 10));   
  269.         Console.WriteLine("All stocks are > .01: " + stocks.All(s => s.CurrentPrice > .01));   
  270.   
  271.         Console.ReadLine();   
  272.   
  273.   
  274.   
  275.         /***********************************************************************  
  276.          *  16. 'RemoveAll' cStock's with CurrentPrice > 25.00  
  277.          *   
  278.          *   
  279.          *  Method(s): RemoveAll  
  280.         ***********************************************************************/  
  281.   
  282.         stocks.RemoveAll(c => c.CurrentPrice > 25);   
  283.   
  284.         foreach (Stock stock in stocks)   
  285.         {   
  286.             OutputFormattedStockToDisplay(stock, ConsoleColor.Green);   
  287.         }   
  288.   
  289.         Console.ReadLine();   
  290.   
  291.   
  292.   
  293.         /***********************************************************************  
  294.          *  17. 'Average' CurrentPrice for each Industry 'GroupBy'  
  295.          *  
  296.          *  Method(s): GroupBy, Average  
  297.         '***********************************************************************/  
  298.   
  299.         foreach (IGrouping<string, Stock> industry in stocks.GroupBy(s => s.Industry.Name))   
  300.         {   
  301.             Console.WriteLine(industry.Average(s => s.CurrentPrice));   
  302.         }   
  303.   
  304.         Console.ReadLine();   
  305.     }   
  306. }  

VB.Net Code Sample

  1. Imports System.Collections.Generic   
  2. Imports LinqSamples   
  3.   
  4. Namespace TestHarnass   
  5.   
  6.     Module Program   
  7.   
  8.         ''' <summary>   
  9.         ''' Determines if stock's PriceChange is greater than zero.   
  10.         ''' </summary>   
  11.         ''' <param name="p_stock">Stock to check.</param>   
  12.         ''' <returns>Boolean indicating if stock's PriceChange is greater than zero.</returns>   
  13.         ''' <remarks></remarks>   
  14.         Public Function GetDollarPlusGainers(ByVal p_stock As cStock) As Boolean  
  15.   
  16.             Return p_stock.PriceChange > 1.0   
  17.   
  18.         End Function  
  19.   
  20.   
  21.   
  22.         Private Sub OutputFormattedStockToDisplay(ByVal p_Stock As cStock, ByVal p_ConsoleColor? As ConsoleColor)   
  23.   
  24.             Console.ForegroundColor = p_ConsoleColor.GetValueOrDefault(ConsoleColor.Yellow)   
  25.   
  26.             Console.WriteLine("StockID: " & p_Stock.StockID)   
  27.             Console.WriteLine("Symbol: " & p_Stock.Symbol)   
  28.             Console.WriteLine("Current Price: " & String.Format("{0:c}", p_Stock.CurrentPrice))   
  29.             Console.WriteLine("Price Change: " & String.Format("{0:c}", p_Stock.PriceChange))   
  30.             Console.WriteLine("IndustryID: " & p_Stock.Industry.IndustryID)   
  31.             Console.WriteLine("Industry Name: " & p_Stock.Industry.Name)   
  32.             Console.WriteLine("")   
  33.             Console.WriteLine("")   
  34.   
  35.         End Sub  
  36.   
  37.   
  38.   
  39.         Sub Main()   
  40.   
  41.             ' Get some stocks   
  42.             Dim stocks As List(Of cStock) = DataHelper.GetTestData()   
  43.   
  44.   
  45.             '***********************************************************************   
  46.             '   1. Get cStocks with PriceChange > 1.00 using linq query   
  47.             '***********************************************************************   
  48.   
  49.             For Each stock As cStock In From s In stocks Where s.PriceChange   
  50.   
  51.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  52.   
  53.             Next  
  54.   
  55.             Console.ReadLine()   
  56.   
  57.   
  58.   
  59.             '***********************************************************************   
  60.             '   2. Get cStocks with PriceChange > 1.00 using delegate   
  61.             '   
  62.             '   Method(s): FindAll   
  63.             '***********************************************************************   
  64.   
  65.             For Each stock As cStock In stocks.FindAll(New Predicate(Of cStock)(AddressOf GetDollarPlusGainers))   
  66.   
  67.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  68.   
  69.             Next  
  70.   
  71.             Console.ReadLine()   
  72.   
  73.   
  74.   
  75.             '***********************************************************************   
  76.             '   3. Get cStocks with PriceChange > 1.00 using Lambda Expression   
  77.             '   
  78.             '   Method(s): FindAll   
  79.             '***********************************************************************   
  80.   
  81.             For Each stock As cStock In stocks.FindAll(Function(s As cStock) s.PriceChange > 1.0)   
  82.   
  83.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  84.   
  85.             Next  
  86.   
  87.             Console.ReadLine()   
  88.   
  89.   
  90.   
  91.             '***********************************************************************   
  92.             '   4. 'Count' cStocks in stocks > $100 and <=$100   
  93.             '   
  94.             '   Method(s): FindAll, Count   
  95.             '***********************************************************************   
  96.   
  97.             Console.WriteLine("Total number of stocks: " & stocks.Count())   
  98.             Console.WriteLine("Number of stocks > $100: " & stocks.FindAll(Function(s As cStock) s.CurrentPrice > 100).Count())   
  99.             Console.WriteLine("Number of stocks <= $100: " & stocks.FindAll(Function(s As cStock) s.CurrentPrice <= 100).Count())   
  100.   
  101.             Console.ReadLine()   
  102.   
  103.   
  104.   
  105.             '***********************************************************************   
  106.             '   5. 'Max' and 'Min' by cStock.Price   
  107.             '   
  108.             '   Method(s): Max, Min   
  109.             '***********************************************************************   
  110.   
  111.             Console.WriteLine("Current Max Stock Price: " & stocks.Max(Function(s As cStock) Convert.ToDouble(s.CurrentPrice)))   
  112.             Console.WriteLine("Current Min Stock Price: " & stocks.Min(Function(s As cStock) Convert.ToDouble(s.CurrentPrice)))   
  113.   
  114.             Console.ReadLine()   
  115.   
  116.   
  117.   
  118.             '***********************************************************************   
  119.             '   6. 'Average' CurrentPrice of all cStocks in stocks   
  120.             '   
  121.             '   Method(s): Average   
  122.             '***********************************************************************   
  123.   
  124.             Console.WriteLine("Avg Current Stock Price: " & stocks.Average(Function(s As cStock) Convert.ToDouble(s.CurrentPrice)))   
  125.   
  126.             Console.ReadLine()   
  127.   
  128.   
  129.   
  130.             '***********************************************************************   
  131.             '   7. 'OrderBy' cStock.Symbol   
  132.             '   
  133.             '   Method(s): FindAll, Count   
  134.             '***********************************************************************   
  135.   
  136.             For Each stock As cStock In stocks.OrderBy(Function(s As cStock) s.Industry.Name)   
  137.   
  138.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  139.   
  140.             Next  
  141.   
  142.             Console.ReadLine()   
  143.   
  144.   
  145.   
  146.             '***********************************************************************   
  147.             '   8. 'OrderBy' cStock.Industry.Name, 'ThenBy' cStock.Symbol   
  148.             '   
  149.             '   Method(s): OrderBy, ThenBy   
  150.             '***********************************************************************   
  151.   
  152.             For Each stock As cStock In stocks.OrderBy(Function(s As cStock) s.Industry.Name).ThenBy(Function(s As cStock) s.Symbol)   
  153.   
  154.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  155.   
  156.             Next  
  157.   
  158.             Console.ReadLine()   
  159.   
  160.   
  161.   
  162.             '***********************************************************************   
  163.             '   9. 'OrderByDescending' cStock.Symbol   
  164.             '   
  165.             '   Method(s): OrderByDescending   
  166.             '***********************************************************************   
  167.   
  168.             For Each stock As cStock In stocks.OrderByDescending(Function(s As cStock) s.Industry.Name)   
  169.   
  170.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  171.   
  172.             Next  
  173.   
  174.             Console.ReadLine()   
  175.   
  176.   
  177.   
  178.             '***********************************************************************   
  179.             '   10. 'Sort' stocks by Symbol (ASC)   
  180.             '   
  181.             '   Method(s): Sort, CompareTo   
  182.             '***********************************************************************   
  183.   
  184.             stocks.Sort(Function(x, y) x.Symbol.CompareTo(y.Symbol))   
  185.   
  186.             For Each stock As cStock In stocks   
  187.   
  188.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  189.   
  190.             Next  
  191.   
  192.             Console.ReadLine()   
  193.   
  194.   
  195.   
  196.             '***********************************************************************   
  197.             '   11. Does stocks 'Contain' any cStocks.CurrentPrice > $100, $150   
  198.             '   
  199.             '   Method(s): Contains, Find   
  200.             '***********************************************************************   
  201.   
  202.             Console.WriteLine("The stocks collection contains stocks > $100: " & stocks.Contains(stocks.Find(Function(s As cStock) s.CurrentPrice > 100)))   
  203.             Console.WriteLine("The stocks collection contains stocks > $150: " & stocks.Contains(stocks.Find(Function(s As cStock) s.CurrentPrice > 150)))   
  204.   
  205.             Console.ReadLine()   
  206.   
  207.   
  208.   
  209.             '***********************************************************************   
  210.             '   12. List cStock's in stocks.CurrentPrice > $50, $100   
  211.             '   
  212.             '   Method(s): OrderByDescending, TakeWhile   
  213.             '***********************************************************************   
  214.   
  215.             For Each stock As cStock In stocks.OrderByDescending(Function(s As cStock) Math.Abs(s.CurrentPrice)).TakeWhile(Function(s As cStock) s.CurrentPrice > 50)   
  216.   
  217.                 Console.WriteLine("The stocks > $50")   
  218.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  219.   
  220.             Next  
  221.   
  222.             Console.WriteLine("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-")   
  223.   
  224.             For Each stock As cStock In stocks.OrderByDescending(Function(s As cStock) Math.Abs(s.CurrentPrice)).TakeWhile(Function(s As cStock) s.CurrentPrice > 100)   
  225.   
  226.                 Console.WriteLine("The stocks > $50")   
  227.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  228.   
  229.             Next  
  230.   
  231.             Console.WriteLine("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-")   
  232.   
  233.             Console.ReadLine()   
  234.   
  235.   
  236.             '***********************************************************************   
  237.             '   13. 'Take' the top 3 stocks by Pricechange   
  238.             '   
  239.             '   Method(s): OrderByDescending, Take   
  240.             '***********************************************************************   
  241.   
  242.             For Each stock As cStock In stocks.OrderByDescending(Function(s As cStock) Math.Abs(s.PriceChange)).Take(3)   
  243.   
  244.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  245.   
  246.             Next  
  247.   
  248.             Console.ReadLine()   
  249.   
  250.   
  251.   
  252.             '***********************************************************************   
  253.             '   14. Elements in stocks that are 'OfType' cStock (all in our case)   
  254.             '   
  255.             '   Method(s): OfType   
  256.             '***********************************************************************   
  257.   
  258.             For Each stock As cStock In stocks.OfType(Of cStock)()   
  259.   
  260.                 Console.WriteLine(stock.Symbol & " is a cStock")   
  261.   
  262.             Next  
  263.   
  264.             Console.ReadLine()   
  265.   
  266.   
  267.             '***********************************************************************   
  268.             '   15. Do 'All' cStock's in stocks have CurrentPrice > 10.00, .01   
  269.             '   
  270.             '   Method(s): All   
  271.             '***********************************************************************   
  272.   
  273.             Console.WriteLine("All stocks are > 10.00: " & stocks.All(Function(s As cStock) s.CurrentPrice > 10.0))   
  274.             Console.WriteLine("All stocks are > .01: " & stocks.All(Function(s As cStock) s.CurrentPrice > 0.01))   
  275.   
  276.             Console.ReadLine()   
  277.   
  278.   
  279.   
  280.             '***********************************************************************   
  281.             '   16. 'RemoveAll' cSstock's with CurrentPrice > 25.00   
  282.             '   
  283.             '   Method(s): RemoveAll   
  284.             '***********************************************************************   
  285.   
  286.             stocks.RemoveAll(Function(s As cStock) s.CurrentPrice > 25)   
  287.   
  288.             For Each stock As cStock In stocks   
  289.   
  290.                 OutputFormattedStockToDisplay(stock, ConsoleColor.Green)   
  291.   
  292.             Next  
  293.   
  294.             Console.ReadLine()   
  295.   
  296.   
  297.   
  298.             '***********************************************************************   
  299.             '   17. 'Average' CurrentPrice for each Industry 'GroupBy'   
  300.             '   
  301.             '   Method(s): GroupBy, Average   
  302.             '***********************************************************************   
  303.   
  304.             For Each industry As IGrouping(Of String, cStock) In stocks.GroupBy(Function(s As cStock) s.Industry.Name)   
  305.                 Console.WriteLine(industry.Average(Function(s As cStock) s.CurrentPrice))   
  306.             Next  
  307.   
  308.             Console.ReadLine()   
  309.   
  310.   
  311.   
  312.             '***********************************************************************   
  313.             '   18. Comma Delimited list of all cStock's in stocks   
  314.             '   
  315.             '   Method(s): ConvertAll, ToArray   
  316.             '***********************************************************************   
  317.   
  318.             Console.WriteLine(String.Join(",", stocks.ConvertAll(Function(s As cStock) s.Symbol).ToArray()))   
  319.   
  320.             Console.ReadLine()   
  321.   
  322.         End Sub  
  323.   
  324.     End Module  
  325.   
  326. End Namespace  

Useful Links:

MSDN List(T) Methods
MSDN Lambda Expressions (C#)
MSDN Lambda Expressions (VB.Net)
The New Lambda Expressions Feature in C# 3.0 Lambda Expressions

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Another Code Site

Things I don't want to forget...so i'm putting them here.