Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Basics of LINQ & Lamda Expressions

, 28 Feb 2009 CPOL
Basic Examples to work with Language Integrated Query(LINQ) concept that was introduces with C# 3.0 and/or .NET 3.5 (Corresponding VB.NET code included)
CoolCode_SRC.zip
LinqBasic
LinqBasic
bin
Debug
LinqBasic.vshost.exe
LinqBasic.vshost.exe.manifest
obj
Debug
TempPE
Properties
Settings.settings
LinqBasic.suo
LinqBasic_src.zip
LinqBasic.vshost.exe
LinqBasic.vshost.exe.manifest
LinqBasic.exe
LinqBasic.pdb
LinqBasic.Form2.resources
LinqBasic.Properties.Resources.resources
LinqBasic.pdb
LinqBasic.exe
LinqBasic.csproj.GenerateResource.Cache
Settings.settings
LinqBasic.suo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqBasic
{
    public partial class Form2 : Form
    {
        List<Employee> employees = new List<Employee>();
        List<Employee> employees1 = new List<Employee>();
        List<Order> orders = new List<Order>();
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            employees.Add(new Employee { age = 40, name = "Basob" });
            employees.Add(new Employee { age = 34, name = "Abhishek" });
            employees.Add(new Employee { age = 65, name = "Souvik" });
            employees.Add(new Employee { age = 65, name = "Ayan" });
            employees.Add(new Employee { age = 68, name = "Raj" });
            employees1.Add(new Employee { age = 68, name = "Pallab" });
            employees1.Add(new Employee { age = 55, name = "Swarup" });
            employees1.Add(new Employee { age = 68, name = "Ranjit" });
            employees1.Add(new Employee { age = 68, name = "Bratin" });
            orders.Add(new Order { empName = "Raj", itemName = "Pen" });
            orders.Add(new Order { empName = "Souvik", itemName = "Pencil" });
            orders.Add(new Order { empName = "Raj", itemName = "Rubber" });
            listBox1.DataSource = employees;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            List<Employee> filteredList = new List<Employee>();
            //var filterEnumerable = from emp in employees
            //                       where emp.age > 50
            //                       select emp;
            var filterEnumerable = employees.Where<Employee>(emp => emp.age > 50);

            filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filteredList;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<Employee> filteredList = new List<Employee>();
            //var filterEnumerable = from emp in employees
            //                        orderby emp.name descending
            //                        select emp.name;
            var filterEnumerable = employees.Select<Employee, string>(r => r.name);

            //filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filterEnumerable.ToList<string>();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<Employee> filteredList = new List<Employee>();
            var filterEnumerable = (from emp in employees
                                    select emp).Take<Employee>(2);
                                   
            //var filterEnumerable = employees.Select<Employee, string>(r => r.name);

            //filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<Employee> filteredList = new List<Employee>();
            //var filterEnumerable = (from emp in employees
            //                        select emp).SkipWhile<Employee>(r => r.name.Length > 4);

            var filterEnumerable = employees.Select<Employee, Employee>(r => { return r; }).SkipWhile<Employee>(r => r.name.Length > 4);

            //filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            List<Employee> filteredList = new List<Employee>();
            //var filterEnumerable = from emp in employees
            //                       join ord in orders on new { Name = emp.name } equals new { Name = ord.empName }
            //                       select emp;

            var filterEnumerable = employees.Join<Employee, Order, string, Employee>(orders, e1 => e1.name, o => o.empName, (o, e2) => o);

            //filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<Employee> filteredList = new List<Employee>();
            //var filterEnumerable = (from emp in employees
            //                       select emp).Concat<Employee>(from emp in employees1
            //                       select emp);

            var filterEnumerable = employees.Concat<Employee>(employees1.AsEnumerable<Employee>());

            //filteredList = filterEnumerable.ToList();
            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //var filterEnumerable = from emp in employees
            //                       group emp by emp.age into gr
            //                       select new { Category = gr.Key, Items = gr };
            //listBox2.DataSource = filterEnumerable.ToList<int>();

            var filterEnumerable2 = from emp in employees
                                    where emp.age > 65
                                    group emp by emp.age into gr
                                    where gr.Key > 40
                                    select new { aaa = gr.Key, ccc = gr.Count<Employee>(), ddd = gr.Sum<Employee>(r => r.age), bbb = gr };
            listBox2.DataSource = (from r in filterEnumerable2 select r.aaa).ToList<int>();
            
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var filterEnumerable = from emp in employees
                                   where emp.name.StartsWith("R")
                                   from ord in orders 
                                   where ord.itemName.StartsWith("R")
                                   orderby ord.itemName descending
                                   select emp;

            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var filterEnumerable = employees.MyOwnWhere<Employee>(r => r.age > 25);
            listBox2.DataSource = filterEnumerable.ToList<Employee>();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            var min = employees.GetMinVal<Employee>(r => r.age);
            listBox2.Items.Add(min.ToString());
        }



    }
    public class Employee
    {
        public string name;
        public int age;
        public override string ToString()
        {
            return this.name;
        }
    }
    public class Order
    {
        public string itemName;
        public string empName;
        public override string ToString()
        {
            return this.itemName;
        }
    }
    public static class Extension
    {

        public static IEnumerable<TSource> MyOwnWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : class
        {
            foreach (var item in source)
            {
                if (predicate(item))
                    yield return item;
                else
                    yield break;
            }
        }

        public static int GetMinVal<TSource>(this IEnumerable<TSource> source,
                                    Func<TSource, int> selector)
        {
            int smallest = int.MaxValue;
            foreach (var item in source)
            {
                int nextItem = selector(item);
                if (nextItem < smallest)
                {
                    smallest = nextItem;
                }
            }
            return smallest;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhishek Sur
Architect
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150331.1 | Last Updated 1 Mar 2009
Article Copyright 2009 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid