Click here to Skip to main content
11,635,656 members (83,084 online)
Click here to Skip to main content

Tagged as

Few extension methods of IEnumerable in C#

, 5 Jul 2011 CPOL 19.2K 4
Rate this:
Please Sign up or sign in to vote.
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.

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

Usage:
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.

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

Usage:
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.

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

Usage:
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.

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

Usage:
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.

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

Usage:
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.

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

Usage:
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.

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:
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.

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:
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.

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:
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)

Share

About the Author


You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 5 Jul 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid