Linq Custom Extension Method With Func Delegate

In this Article we will create custom extension method for linq using Func delegate for performing logical operations to get even numbers, odd numbers, up-to a particular range and many more over collection.

You should know about before proceed further.

1) delegate

2) Func delegate

3) Extension Methods

Step 1: Create Static Class i.e. CustomExtensions

namespace CustomExtensions { public static class LinqExtension { } }

Step 2: Create Static Extension Method i.e. LogicalOperation of return type IEnumerable, which will retun collection of type int, and with same this argument, because it will performing operation over collection of type int too. Add one more argument with Func delegate. Which will take one int type argument and return bool type result.

Add two system references for type and generic collection.
using System;
using System.Collections.Generic;

namespace SWClass.CustomExtensions { using System; using System.Collections.Generic; public static class LinqExtension { public static IEnumerable LogicalOperation(this IEnumerable colect,Func deleg) { } } }

Step 3: In static method LogicalOperation of static class LinqExtension, we will create a temporary collection for reutrn filter result after delegate operation. Iterate entire input collection and apply Func delegate operation to validate each element of collection. and return the filtered collection.

namespace SWClass.CustomExtensions { using System; using System.Collections.Generic; public static class LinqExtension { public static IEnumerable LogicalOperation(this IEnumerable colect,Func fDeleg) { List lst = new List(); foreach (int i in colect) if (fDeleg(i)) lst.Add(i); return lst; } } }

Step 3: Now We will test this Custom Linq Extension method for getting even numbers list. Don't forget to add Reference of SWClass.CustomExtensions.

namespace SWClass.Application { using System; using System.Collections.Generic; using System.Linq; using SWClass.CustomExtensions; class Program { static void Main(string[] args) { IEnumerable l = Enumerable.Range(1, 1000); // Calling Custom Linq Extension Method List fl = l.LogicalOperation(x => x % 2 == 0).ToList(); foreach (int i in fl) Console.WriteLine(i); Console.ReadKey(); } } }

Moreover We can Check Limits Up to 100 as follows.

namespace SWClass.Application { using System; using System.Collections.Generic; using System.Linq; using SWClass.CustomExtensions; class Program { static void Main(string[] args) { IEnumerable l = Enumerable.Range(1, 1000); List fl = l.LogicalOperation(x => x % 2 == 0).LogicalOperation(x => x <= 100).ToList(); foreach (int i in fl) Console.WriteLine(i); Console.ReadKey(); } } }

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories