Select Collection or Array Elements - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Select Collection or Array Elements

Demo Code

using System;/*from  w  w  w. j av a2  s  .c  o  m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
class MainClass
{
   static void Main(string[] args)
   {
      // create a list of fruit
      List<Item> myList = new List<Item>() {
         new Item("apple", "green"),
         new Item("orange", "orange"),
         new Item("file", "yellow"),
         new Item("mango", "yellow"),
         new Item("cherry", "red"),
         new Item("fig", "brown"),
         new Item("new", "red"),
         new Item("pear", "green")
      };
      IEnumerable<string> myResult = from e in myList where e.Color != "red" && e.Name[0] != 'c' orderby e.Name select e.Name;
      foreach (string result in myResult)
      {
         Console.WriteLine("Result: {0}", result);
      }
      myResult = myList.Where(e => e.Color != "red" && e.Name[0] != 'c').OrderBy(e => e.Name).Select(e => e.Name);
      foreach (string result in myResult)
      {
         Console.WriteLine("Lambda Result: {0}", result);
      }
   }
}
class Item
{
   public Item(string nameVal, string colorVal)
   {
      Name = nameVal;
      Color = colorVal;
   }
   public string Name { get; set; }
   public string Color { get; set; }

}

Related Tutorials