Click here to Skip to main content
Click here to Skip to main content

Writing Custom LINQ Extension Methods

By , 17 Jun 2009
 

Microsoft have done a great job with LINQ IMHO. However there are times when it might be handy to create your own LINQ extension methods. LINQ extension methods can be applied to any type provided that the source is of type IEnumerable<T>, so thats really the only requirement.

So how do we write an extension method. Well its quite simple. Here are 2 that I’ve written for a  IEnumerable<string> the first example takes a Predicate and the second one doesnt. This extension method will check whether a file name string is a valid image file, but you can do it for whatever your pupose is.

customlinq.png

The only other thing to note is the cryptic “this” keyword used in the method signature. This means its being applied to the current IEnumerable<T> collection. Just always put it in your extension methods and you’ll be fine.

So how to use these extension methods, well providing your dealing with a  Enumerable<string> collection youll get the option coming up as an extension method.

vs2008linq.png

And to actually use it you could use a standard LINQ query something like

usingcustomlinq.png

License

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

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
Member
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
 
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
 
Both of these at Sussex University UK.
 
Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralTnxmemberBostjan Gajsler29 Apr '11 - 0:29 
Thank you for the great demonstration. It helped me alot.
 
Bostjan Gajsler
GeneralExtension method errormemberwilfredogr16 Mar '11 - 4:00 
Hi Sacha
I read your article and I created a extension method for search with removing diacritics (àéèêî...), but i have an error: Method 'System.String RemoveDiacritics(System.String)' has no supported translation to SQL.
 
---------- Here is my method.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
public static class ExtensionMethods
{
public static string RemoveDiacritics(this string text)
{
string ret = "";
ret = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding(1251).GetBytes(text));
return ret;
}
}

 
---------- Here my line of code linq
var result30 = from a in db.avocats where ( (a.Nom.RemoveDiacritics() ?? "").Contains(vNom) ) select a;
 
Can you help me please with this problem? Thank you.
 
Regards,
Wilfredo.
GeneralSome comments...memberOleg Shilo17 Jun '09 - 14:41 
This is a great demonstration of Extension Methods power.
 
I would only change the names. IsImageFile is more appropriate for Predicates but not for "filters". And filtering is what your IsImageFile is doing.
 
I think something as GetImages or AllImages would work better:
 
var imgs = (from fi in files select fi.FullName)).
            AllImageFiles().Skip(....
 
I would also make more emphasis on Closure. Technically yielding in your example is optional as building and returning the whole result collection is appropriate too. However using closure is more flexible when the Extension Method is used in loops (for/foreach).
 
Regards,

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 17 Jun 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid