.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
ASPEvil
Jean Paul
Ashutosh Jha
satyapriyanayak
SP

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

LINQ Enumerable

Posted By:Karthikeyan Anbarasan       Posted Date: January 02, 2011    Points: 10    Category: LINQ    URL: http://www.f5debug.net/  

This example demonstrate the usage of LINQ Enumerable in .Net 4.0
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            SetOperations();
            Untyped();
        }

        static void Untyped()
        {
            var list = new System.Collections.ArrayList(Formula1.GetChampions() as System.Collections.ICollection);

            var query = from r in list.Cast()
                        where r.Country == "UK"
                        orderby r.Wins descending
                        select r;
            foreach (var racer in query)
            {
                Console.WriteLine("{0:A}", racer);
            }

        }

        static void UsingZIP()
        {
            var racerNames = from r in Formula1.GetChampions()
                             where r.Country == "Italy"
                             orderby r.Wins descending
                             select new
                             {
                                 Name = r.FirstName + " " + r.LastName
                             };

            var racerNamesAndStarts = from r in Formula1.GetChampions()
                                   where r.Country == "Ireland"
                                   orderby r.Wins descending
                                   select new
                                   {
                                       LastName = r.LastName,
                                       Starts = r.Starts
                                   };
        }

        static void UsingAggre()
        {
            var countries = (from c in
                                 from r in Formula1.GetChampions()
                                 group r by r.Country into c
                                 select new
                                 {
                                     Country = c.Key,
                                     Wins = (from r1 in c
                                             select r1.Wins).Sum()
                                 }
                             orderby c.Wins descending, c.Country
                             select c).Take(5);

            foreach (var country in countries)
            {
                Console.WriteLine("{0} {1}", country.Country, country.Wins);
            }

        }

        static void UsingAggre1()
        {
            var query = from r in Formula1.GetChampions()
                        where r.Years.Count() > 3
                        orderby r.Years.Count() descending
                        select new
                        {
                            Name = r.FirstName + " " + r.LastName,
                            TimesChampion = r.Years.Count()
                        };

            foreach (var r in query)
            {
                Console.WriteLine("{0} {1}", r.Name, r.TimesChampion);
            }
        }

        static void UsingPartitioning()
        {
            int pageSize = 5;

            int numberPages = (int)Math.Ceiling(Formula1.GetChampions().Count() /
                  (double)pageSize);

            for (int page = 0; page < numberPages; page++)
            {
                Console.WriteLine("Page {0}", page);

                var racers =
                   (from r in Formula1.GetChampions()
                    orderby r.LastName
                    select r.FirstName + " " + r.LastName).
                   Skip(page * pageSize).Take(pageSize);

                foreach (var name in racers)
                {
                    Console.WriteLine(name);
                }
                Console.WriteLine();
            }

        }

        static void SetOperations()
        {
            Func> racersByCar =
                car => from r in Formula1.GetChampions()
                       from c in r.Cars
                       where c == car
                       orderby r.LastName
                       select r;

            Console.WriteLine("World champion with Ferrari and McLaren");
            foreach (var racer in racersByCar("Huyndai").Intersect(racersByCar("Ford")))
            {
                Console.WriteLine(racer);
            }
        }

        static void Join()
        {
            var racers = from r in Formula1.GetChampions()
                         from y in r.Years
                         where y > 2003
                         select new
                         {
                             Year = y,
                             Name = r.FirstName + " " + r.LastName
                         };

            var teams = from t in
                            Formula1.GetContructorChampions()
                        from y in t.Years
                        where y > 2003
                        select new
                        {
                            Year = y,
                            Name = t.Name
                        };

            var racersAndTeams =
                  from r in racers
                  join t in teams on r.Year equals t.Year
                  select new
                  {
                      Year = r.Year,
                      Racer = r.Name,
                      Team = t.Name
                  };

            Console.WriteLine("Year  Champion " + "Constructor Title");
            foreach (var item in racersAndTeams)
            {
                Console.WriteLine("{0}: {1,-20} {2}",
                   item.Year, item.Racer, item.Team);
            }

        }


        static void GroupingWithNestedObjects()
        {
            var countries = from r in Formula1.GetChampions()
                            group r by r.Country into g
                            orderby g.Count() descending, g.Key
                            where g.Count() >= 2
                            select new
                            {
                                Country = g.Key,
                                Count = g.Count(),
                                Racers = from r1 in g
                                         orderby r1.LastName
                                         select r1.FirstName + " " + r1.LastName
                            };
            foreach (var item in countries)
            {
                Console.WriteLine("{0, -10} {1}", item.Country, item.Count);
                foreach (var name in item.Racers)
                {
                    Console.Write("{0}; ", name);
                }
                Console.WriteLine();
            }

        }

        static void Grouping()
        {
            var countries = from r in Formula1.GetChampions()
                            group r by r.Country into g
                            orderby g.Count() descending, g.Key
                            where g.Count() >= 2
                            select new
                            {
                                Country = g.Key,
                                Count = g.Count()
                            };

            foreach (var item in countries)
            {
                Console.WriteLine("{0, -10} {1}", item.Country, item.Count);
            }

        }

        static void CompoundFrom()
        {
            var ferrariDrivers = from r in Formula1.GetChampions()
                                 from c in r.Cars
                                 where c == "Ford"
                                 orderby r.LastName
                                 select r.FirstName + " " + r.LastName;

            foreach (var racer in ferrariDrivers)
            {
                Console.WriteLine(racer);
            }

        }

        static void TypeFiltering()
        {
            object[] data = { 1, "two", 3, "four", "five", 6 };
            var query = data.OfType();
            foreach (var s in query)
            {
                Console.WriteLine(s);
            }

        }

        static void IndexFiltering()
        {
            var racers = Formula1.GetChampions().
                    Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0);
            foreach (var r in racers)
            {
                Console.WriteLine("{0:A}", r);
            }

        }


        static void Filtering()
        {
            var racers = from r in Formula1.GetChampions()
                         where r.Wins > 15 && (r.Country == "US" || r.Country == "UK")
                         select r;

            foreach (var r in racers)
            {
                Console.WriteLine("{0:A}", r);
            }

        }
    }
}




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend