Return to Snippet

Revision: 27561
at June 24, 2010 12:50 by stewshack


Updated Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace ExampleLinq
{
    public class StarWarsCharacter
    {
        public string Name { get; set; }
        public CharacterType CharacterType { get; set; }
    }

    public enum CharacterType 
    { 
        Hero,
        Villain
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List<StarWarsCharacter> starWarsCharacters = new List<StarWarsCharacter>()
               {
                   new StarWarsCharacter { Name = "Luke", CharacterType = CharacterType.Hero },
                   new StarWarsCharacter { Name = "Vader", CharacterType = CharacterType.Villain },
                   new StarWarsCharacter { Name = "Obi-Wan", CharacterType = CharacterType.Hero },
                   new StarWarsCharacter { Name = "Tarkin", CharacterType = CharacterType.Villain }
               };
            var heroes = from s in starWarsCharacters
                         where s.CharacterType == CharacterType.Hero
                         select s.Name;
            heroes.ToList<string>().ForEach(h => Console.WriteLine(h));
        }
    }
}

Revision: 27560
at June 16, 2010 15:10 by stewshack


Updated Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace ExampleLinq
{
   public class StarWarsCharacter
   {
      public string Name { get; set; }
      public string CharacterType { get; set; }
   }

   public class Program
   {
      static void Main(string[] args)
      {
         List<StarWarsCharacter> starWarsCharacters = new List<StarWarsCharacter>()
	 {
	    new StarWarsCharacter { Name = "Luke", CharacterType = "Hero" },
	    new StarWarsCharacter { Name = "Vader", CharacterType = "Villain" },
	    new StarWarsCharacter { Name = "Obi-Wan", CharacterType = "Hero" },
	    new StarWarsCharacter { Name = "Tarkin", CharacterType = "Villain" }
	 };
           
         var heroes = from s in starWarsCharacters
            where s.CharacterType == "Hero"
            select s.Name;

         heroes.ToList<string>().ForEach(h => Console.WriteLine(h));
      }
   }
}

Revision: 27559
at June 16, 2010 15:09 by stewshack


Updated Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace ExampleLinq
{
   public class StarWarsCharacter
   {
      public string Name { get; set; }
      public string CharacterType { get; set; }
   }

   public class Program
   {
      static void Main(string[] args)
      {
         List<StarWarsCharacter> starWarsCharacters = new List<StarWarsCharacter>()
	 {
	    new StarWarsCharacter { Name = "Luke", CharacterType = "Hero" },
	    new StarWarsCharacter { Name = "Vader", CharacterType = "Villain" },
	    new StarWarsCharacter { Name = "Obi Wan", CharacterType = "Hero" },
	    new StarWarsCharacter { Name = "Tarkin", CharacterType = "Villain" }
	 };
           
         var heroes = from s in starWarsCharacters
            where s.CharacterType == "Hero"
            select s.Name;

         heroes.ToList<string>().ForEach(h => Console.WriteLine(h));
      }
   }
}

Revision: 27558
at June 16, 2010 15:07 by stewshack


Updated Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace ExampleLinq
{
	public class StarWarsCharacter
    {
        public string Name { get; set; }
        public string CharacterType { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List<StarWarsCharacter> starWarsCharacters = new List<StarWarsCharacter>()
				{
					new StarWarsCharacter { Name = "Luke", CharacterType = "Hero" },
					new StarWarsCharacter { Name = "Vader", CharacterType = "Villain" },
					new StarWarsCharacter { Name = "Obi Wan", CharacterType = "Hero" },
					new StarWarsCharacter { Name = "Tarkin", CharacterType = "Villain" }
				};

            var heroes = from s in starWarsCharacters
                         where s.CharacterType == "Hero"
                         select s.Name;

            heroes.ToList<string>().ForEach(h => Console.WriteLine(h));
        }
    }
}

Revision: 27557
at June 16, 2010 14:59 by stewshack


Initial Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace ExampleLinq
{
	public class StarWarsCharacter
	{
		public string name { get; set; }
		public string characterType { get; set; }
	}

	public class Program
	{
		static void Main( string[] args )
		{
			List<StarWarsCharacter> starWarsCharacters = new List<StarWarsCharacter>()
				{
					new StarWarsCharacter { name = "Luke", characterType = "Hero" },
					new StarWarsCharacter { name = "Vader", characterType = "Villan" },
					new StarWarsCharacter { name = "Obi Wan", characterType = "Hero" },
					new StarWarsCharacter { name = "Tarkin", characterType = "Villan" }
				};

			var heroes = from s in starWarsCharacters
				where s.characterType == "Hero"
				select s.name;

			foreach ( string hero in heroes )
			{
				Console.WriteLine( hero );
			}
		}
	}
}

Initial URL

                                

Initial Description
This is a simple Linq query against a list of Star Wars characters.

Initial Title
Linq to Star Wars

Initial Tags
c

Initial Language
C#