Using "Where" method (of Enumerable/LINQ) with Actions, delegates and Lambdas

by Jagadish Pulakhandam on 4/11/2012 7:39:06 AM
Rated 0 from 0 votes
Brief: Demonstrates 5+ different ways to work with "Where" method (available in Enumerable/LINQ) including delegates, anonymous methods, inline delegates, Function delegate, Lambda syntax etc.
Posted to: LINQ operator (extension method) - 'Where' (examples)
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Following source code explains the following (with respect to Enumerable.Where() extension method using LINQ):
  • How to use "Function" delegate (with a custom method)
  • How to use "Function" delegate with anonymous method
  • How to use "Function" delegate directly with "Where" extension method
  • How to use "Function" delegate with anonymous method using lambda syntax (both long & short forms)
  • How to pass anonymous function as a parameter to "Where" method (both delegate and Lambda syntax)
  • How to use a custom method with "Where" method
  • How to pass a Function delegate as a parameter/argument to a method
  • How to pass a lambda expression (Function based delegate) as a parameter/argument to a method

Screen shot:



Source Code:

Method 1:

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 WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
 
        List<Employee> lstEmployees;
        private void Form3_Load(object sender, EventArgs e)
        {
            lstEmployees = new List<Employee>() {
                new Employee() {Empno = 1001, Ename="Jag", Sal=4500},
                new Employee() {Empno = 1002, Ename="Chat", Sal=5600},
                new Employee() {Empno = 1003, Ename="Dhana", Sal=7800},
                new Employee() {Empno = 1004, Ename="Winner", Sal=3400},
                new Employee() {Empno = 1005, Ename="Scott", Sal=4400},
                new Employee() {Empno = 1006, Ename="Smith", Sal=6400}
            };
             
            //Function delegate with a custom method
            //-------------------------------------------------------------------------
            var q = new Func<Employee, Boolean>(SalAbove4500);
            dataGridView1.DataSource = lstEmployees.Where(q).ToList();
        }
 
        private bool SalAbove4500(Employee e)
        {
            return e.Sal > 4500;
        }
    }
}

Method 2:

//Function delegate with anonymous method
//-------------------------------------------------------------------------
var q = new Func<Employee, Boolean>(delegate(Employee o) { return o.Sal > 4500; });
dataGridView1.DataSource = lstEmployees.Where(q).ToList();

Method 3:

//Function delegate instance (with anonymous method) as argument to "Where"
//-------------------------------------------------------------------------
dataGridView1.DataSource = lstEmployees.Where(new Func<Employee, Boolean>(delegate(Employee o) { return o.Sal > 4500; })).ToList();

Method 4:

//Function delegate with anonymous method using lambda syntax
//----------------------------------------------------------
var q = new Func<Employee, Boolean>((Employee o) => { return o.Sal > 4500; });
dataGridView1.DataSource = lstEmployees.Where(q).ToList();

Method 5:

//Function delegate with anonymous method using lambda syntax (short form)
//----------------------------------------------------------------------
var q = new Func<Employee, Boolean>(o => o.Sal > 4500);
dataGridView1.DataSource = lstEmployees.Where(q).ToList();

Method 6:

//Passing an anonymous method (delegate) to "Where"
//----------------------------------------------------------------------
dataGridView1.DataSource = lstEmployees.Where(delegate(Employee p) { return p.Sal > 4500; }).ToList();

Method 7:

//Passing an anonymous method (using lambda syntax) to "Where"
//--------------------------------------------------------------
dataGridView1.DataSource = lstEmployees.Where((Employee p) => { return p.Sal > 4500; }).ToList();

Method 8:

//Passing an anonymous method (using lambda syntax) to "ForEach" (short form)
//---------------------------------------------------------------------------
dataGridView1.DataSource = lstEmployees.Where(p => p.Sal > 4500).ToList();

Method 9:

    //Passing a custom method to "Where"
    //-----------------------------------
    dataGridView1.DataSource = lstEmployees.Where(GetSalAbove4500Expression()).ToList();
 
private Func<Employee, Boolean> GetSalAbove4500Expression()
{
    return new Func<Employee, Boolean>(o => o.Sal > 4500);
}

Method 10:

    //Passing a function delegate to a method
    //--------------------------------------
    var q = new Func<Employee, Boolean>((Employee o) => { return o.Sal > 4500; });
    dataGridView1.DataSource = GetEmployees(q).ToList();
 
private IEnumerable<Employee> GetEmployees(Func<Employee, bool> q)
{
    return lstEmployees.Where(q);
}

Method 11:

    //Passing an anonymous function delegate to a method using lambda syntax
    //-----------------------------------------------------------------------------
    dataGridView1.DataSource = GetEmployees(o => o.Sal > 4500).ToList();
 
private IEnumerable<Employee> GetEmployees(Func<Employee, bool> q)
{
    return lstEmployees.Where(q);
}


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Suggested Readings

Attachments / Source Code
You need to Login or Join for FREE to download the following