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

Tagged as

Few extension methods of IEnumerable in C#

, 5 Jul 2011 CPOL
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


Comments and Discussions

 
QuestionFrom IEnumerable(Of T) to DataTable [modified] PinmemberRaja.Lakshman4-Apr-13 16:21 
GeneralReason for my vote of 1 The methods are redundant and easily... PinmemberDaniel Gidman20-Jul-11 8:36 
GeneralReason for my vote of 1 Some of these methods make little se... Pinmembertitogarcia11-Jul-11 23:47 
GeneralRe: Thanks for your comment. PinmemberMohammad A Rahman12-Jul-11 1:10 
GeneralConsider using item.IsNullOrWhiteSpace in IgnoreNullOrEmptyO... Pinmembercerda6-Jul-11 20:40 
GeneralRe: Thanks for your opinion. I will update. :) PinmemberMohammad A Rahman6-Jul-11 21:29 
GeneralShouldn't the IfLengthInRange extension method use the && op... PinmemberGeorge Swan5-Jul-11 0:24 
GeneralRe: Thanks for that :) You are right. I will update that. PinmemberMohammad A Rahman5-Jul-11 0: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.141220.1 | Last Updated 5 Jul 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid