Created
April 26, 2015 11:21
-
-
Save xxli807/1511db53f9f189878404 to your computer and use it in GitHub Desktop.
IEnumableExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///Summary | |
///IEnumableExtension is another common used in c# MVC. In the dropdown List | |
///Summary | |
public static class IEnumerableExtensions | |
{ | |
public static string JoinString(this IEnumerable<string> values) | |
{ | |
return JoinString(values, ","); | |
} | |
public static string JoinString(this IEnumerable<string> values, string split) | |
{ | |
var result = values.Aggregate(string.Empty, (current, value) => current + (split + value)); | |
result = result.TrimStart(split.ToCharArray()); | |
return result; | |
} | |
public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Action<T> action) | |
{ | |
if (source != null) | |
{ | |
foreach (var item in source) | |
{ | |
action(item); | |
} | |
} | |
return source; | |
} | |
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, object> text, Func<T, object> value) | |
{ | |
return source.ToSelectList(text, value, null, null); | |
} | |
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, object> text, Func<T, object> value, string optionalText) | |
{ | |
return source.ToSelectList(text, value, null, optionalText); | |
} | |
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, object> text, Func<T, object> value, Func<T, bool> selected) | |
{ | |
return source.ToSelectList(text, value, selected, null); | |
} | |
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, object> text, Func<T, object> value, Func<T, bool> selected, string optionalText) | |
{ | |
return source.ToSelectList(text, value, selected, optionalText, string.Empty); | |
} | |
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> source, Func<T, object> text, Func<T, object> value, Func<T, bool> selected, string optionalText, string optionalValue) | |
{ | |
var items = new List<SelectListItem>(); | |
if (source == null) | |
{ | |
return items; | |
} | |
foreach (var entity in source) | |
{ | |
var item = new SelectListItem(); | |
item.Text = text(entity).ToString(); | |
item.Value = value(entity).ToString(); | |
if (selected != null) | |
{ | |
item.Selected = selected(entity); | |
} | |
switch (item.Text.ToLower()) | |
{ | |
case "other": | |
case "others": | |
item.Text = " " + item.Text; | |
break; | |
} | |
if (item.Value != optionalValue) | |
{ | |
items.Add(item); | |
} | |
} | |
items = items.OrderBy(d => d.Text).ToList(); | |
var otherItem = items.Where(d => d.Text.ToLower().StartsWith(" other")); | |
otherItem.Each(d => | |
{ | |
d.Text = d.Text.Replace(" ", ""); | |
}); | |
if (!string.IsNullOrEmpty(optionalText)) | |
{ | |
items.Insert(0, new SelectListItem() { Text = optionalText, Value = optionalValue }); | |
} | |
return items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment