Few extension methods of IEnumerable in C#





1.00/5 (2 votes)
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>();