Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static string HandleEngFormat(string word, List<string> list)
- {
- switch (list.Count)
- {
- case 0:
- return string.Empty;
- case 1:
- return list[0];
- case 2:
- return list[0] + " " + word + " " + list[1];
- default:
- if (list.Count < 0)
- {
- throw new Exception("count is less than 0.");
- }
- int last = list.Count - 1;
- list[last] = word + " " + list[last];
- string output = list[0];
- for (int i = 1; i < list.Count; i++)
- {
- output += ", " + list[i];
- }
- return output;
- }
- }
- public static string ToEngFormat<T>(this IEnumerable<T> collection, string word = "and")
- {
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- string conv;
- foreach (T item in collection)
- {
- if (!string.IsNullOrEmpty(conv = item.ToString()))
- {
- list.Add(conv);
- }
- }
- list = new List<string>(collection.Select(item => item.ToString()));
- }
- else
- {
- string conv;
- foreach (T item in collection)
- {
- if (item != null && !string.IsNullOrEmpty(conv = item.ToString()))
- {
- list.Add(conv);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
- public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, bool> where)
- {
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- string conv;
- foreach (T item in collection)
- {
- if (where(item) && !string.IsNullOrEmpty(conv = item.ToString()))
- {
- list.Add(conv);
- }
- }
- }
- else
- {
- string conv;
- foreach (T item in collection)
- {
- if (item != null && where(item) && !string.IsNullOrEmpty(conv = item.ToString()))
- {
- list.Add(conv);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
- public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, string> select)
- {
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- string conv;
- foreach (T item in collection)
- {
- if (!string.IsNullOrEmpty(conv = select(item)))
- {
- list.Add(conv);
- }
- }
- }
- else
- {
- string conv;
- foreach (T item in collection)
- {
- if (item != null && !string.IsNullOrEmpty(conv = select(item)))
- {
- list.Add(conv);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
- public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, bool> where, Func<T, string> select)
- {
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- string conv;
- foreach (T item in collection)
- {
- if (where(item) && !string.IsNullOrEmpty(conv = select(item)))
- {
- list.Add(conv);
- }
- }
- }
- else
- {
- string conv;
- foreach (T item in collection)
- {
- if (item != null && where(item) && !string.IsNullOrEmpty(conv = select(item)))
- {
- list.Add(conv);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
- public static string ToEngFormat<T, T2>(this IEnumerable<T> collection, string word, Func<T, T2> select, Func<T2, bool> where)
- {
- T2 ykno;
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- foreach (T item in collection)
- {
- ykno = select(item);
- if (where(ykno))
- {
- list.Add(ykno.ToString());
- }
- }
- }
- else
- {
- string conv;
- foreach (T item in collection)
- {
- if (item == null || (ykno = select(item)) == null)
- {
- continue;
- }
- if (where(ykno) && !string.IsNullOrEmpty(conv = ykno.ToString()))
- {
- list.Add(conv);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
- public static string ToEngFormat<T, T2>(this IEnumerable<T> collection, string word, Func<T, T2> select, Func<T2, bool> where, Func<T2, string> select2)
- {
- T2 ykno;
- string ykno2;
- List<string> list = new List<string>();
- if (typeof(T).IsValueType)
- {
- foreach (T item in collection)
- {
- ykno = select(item);
- if (where(ykno) && !string.IsNullOrEmpty(ykno2 = select2(ykno)))
- {
- list.Add(ykno2);
- }
- }
- }
- else
- {
- foreach (T item in collection)
- {
- if (item == null || (ykno = select(item)) == null)
- {
- continue;
- }
- if (where(ykno) && !string.IsNullOrEmpty(ykno2 = select2(ykno)))
- {
- list.Add(ykno2);
- }
- }
- }
- return HandleEngFormat(word, list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement