Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C#
Tip/Trick

Few extension methods of IEnumerable in C#

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
5 Jul 2011CPOL1 min read 41.7K   4   8
Few extension methods of IEnumerable in C#
Following are a few extension methods for IEnumerable<string>. Each extension method has a description, code and usage section to describe it.

public static IEnumerable<string> IfMatchWith(this IEnumerable<string> myList, string itemToMatch)


This extension method will iterate through the list and return those items matched with itemToMatch passed by the caller of this method.

C#
public static IEnumerable<string> IfMatchWith(this IEnumerable<string> myList, string itemToMatch)
{
    foreach (var item in myList.Where(item => item == itemToMatch))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "C", "D", "E" };
myList = myList.IfMatchWith("A").ToList<string>();


public static IEnumerable<string> IfNotMatchWith(this IEnumerable<string> myList, string itemToMatch)


This extension method will iterate through the list and return those not matched with itemToMatch passed by caller of this method.

C#
public static IEnumerable<string> IfNotMatchWith(this IEnumerable<string> myList, string itemToMatch)
{
    foreach (var item in myList.Where(item => item != itemToMatch))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "C", "D", "E" };
myList = myList.IfNotMatchWith("A").ToList<string>();


public static IEnumerable<string> IgnoreNullOrEmptyOrSpace(this IEnumerable<string> myList)


This extension method will iterate through the list and return those items only that matched with itemToMatch passed by caller of this method.

C#
public static IEnumerable<string> IgnoreNullOrEmptyOrSpace(this IEnumerable<string> myList)
{
    foreach (var item in myList.Where(item => !string.IsNullOrEmpty(item) && item != " "))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "A", "A", "A", "", " ", "C", null };
myList = myList.IgnoreNullOrEmptyOrSpace().ToList<string>();


public static IEnumerable<string> MakeAllUpper(this IEnumerable<string> myList)


This extension method will iterate through the list and return those items after converting to upper case to the caller of this method.

C#
public static IEnumerable<string> MakeAllUpper(this IEnumerable<string> myList)
{
    foreach (var item in myList)
        yield return item.ToUpper();
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.MakeAllUpper().ToList<string>();


public static IEnumerable<string> MakeAllLower(this IEnumerable<string> myList)


This extension method will iterate through the list and return those items after converting to lower case to the caller of this method.

C#
public static IEnumerable<string> MakeAllLower(this IEnumerable<string> myList)
{
    foreach (var item in myList)
        yield return item.ToLower();
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.MakeAllLower().ToList<string>();


public static IEnumerable<T> MakeAllDefault<T>(this IEnumerable<T> myList)


This extension method will iterate through the list and return default values of those items to the caller of this method.

C#
public static IEnumerable<T> MakeAllDefault<T>(this IEnumerable<T> myList)
{
    foreach (var item in myList)
        yield return default(T);
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.MakeAllDefault<string>().ToList<string>();


public static IEnumerable<string> IfMatchWithPattern(this IEnumerable<string> myList, string pattern)


This extension method will iterate through the list and return those matched with the Pattern to the caller of this method.

C#
public static IEnumerable<string> IfMatchWithPattern(this IEnumerable<string> myList, string pattern)
{
    foreach (var item in myList.Where(item => Regex.IsMatch(item, pattern)))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.IfMatchWithPattern(".").ToList<string>();


public static IEnumerable<string> IfLengthEquals(this IEnumerable<string> myList, int itemLength)


This extension method will iterate through the list and return those items whose length is equal to the itemLength given by the caller of this method.

C#
public static IEnumerable<string> IfLengthEquals(this IEnumerable<string> myList, int itemLength)
{
    foreach (var item in myList.Where(item => item.Length == itemLength))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.IfLengthEquals(1).ToList<string>();


 public static IEnumerable<string> IfLengthInRange(this IEnumerable<string> myList, int startOfRange, int endOfRange)


This extension method will iterate through the list and return those items whose length is in range of startOfRange and endOfRange.

C#
public static IEnumerable<string> IfLengthInRange(this IEnumerable<string> myList, int startOfRange, int endOfRange)
{
    foreach (var item in myList.Where(item => item.Length >= startOfRange && item.Length <= endOfRange))
        yield return item;
}


Usage:
C#
IList<string> myList = new List<string>() { "A", "B", "c", "d", "E" };
myList = myList.IfLengthInRange(1, 2).ToList<string>();

License

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


Written By
Software Developer
Australia Australia

Comments and Discussions

 
QuestionFrom IEnumerable(Of T) to DataTable Pin
Raja.Lakshman4-Apr-13 15:21
Raja.Lakshman4-Apr-13 15:21 
GeneralReason for my vote of 1 The methods are redundant and easily... Pin
Daniel Gidman20-Jul-11 7:36
professionalDaniel Gidman20-Jul-11 7:36 
GeneralReason for my vote of 1 Some of these methods make little se... Pin
titogarcia11-Jul-11 22:47
titogarcia11-Jul-11 22:47 
GeneralRe: Thanks for your comment. Pin
Mohammad A Rahman12-Jul-11 0:10
Mohammad A Rahman12-Jul-11 0:10 
GeneralConsider using item.IsNullOrWhiteSpace in IgnoreNullOrEmptyO... Pin
cerda6-Jul-11 19:40
cerda6-Jul-11 19:40 
GeneralRe: Thanks for your opinion. I will update. :) Pin
Mohammad A Rahman6-Jul-11 20:29
Mohammad A Rahman6-Jul-11 20:29 
GeneralShouldn't the IfLengthInRange extension method use the && op... Pin
George Swan4-Jul-11 23:24
mveGeorge Swan4-Jul-11 23:24 
GeneralRe: Thanks for that :) You are right. I will update that. Pin
Mohammad A Rahman4-Jul-11 23:35
Mohammad A Rahman4-Jul-11 23:35 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.