Advertisement
Nicktay24

IEnumerables to EngFormat ({0}, {1}, and/or {2})

May 28th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.86 KB | None | 0 0
  1.  
  2.  
  3.         private static string HandleEngFormat(string word, List<string> list)
  4.         {
  5.             switch (list.Count)
  6.             {
  7.                 case 0:
  8.                     return string.Empty;
  9.                 case 1:
  10.                     return list[0];
  11.                 case 2:
  12.                     return list[0] + " " + word + " " + list[1];
  13.                 default:
  14.                     if (list.Count < 0)
  15.                     {
  16.                         throw new Exception("count is less than 0.");
  17.                     }
  18.                     int last = list.Count - 1;
  19.                     list[last] = word + " " + list[last];
  20.                     string output = list[0];
  21.                     for (int i = 1; i < list.Count; i++)
  22.                     {
  23.                         output += ", " + list[i];
  24.                     }
  25.                     return output;
  26.             }
  27.         }
  28.  
  29.         public static string ToEngFormat<T>(this IEnumerable<T> collection, string word = "and")
  30.         {
  31.             List<string> list = new List<string>();
  32.             if (typeof(T).IsValueType)
  33.             {
  34.                 string conv;
  35.                 foreach (T item in collection)
  36.                 {
  37.                     if (!string.IsNullOrEmpty(conv = item.ToString()))
  38.                     {
  39.                         list.Add(conv);
  40.                     }
  41.                 }
  42.                 list = new List<string>(collection.Select(item => item.ToString()));
  43.             }
  44.             else
  45.             {
  46.                 string conv;
  47.                 foreach (T item in collection)
  48.                 {
  49.                     if (item != null && !string.IsNullOrEmpty(conv = item.ToString()))
  50.                     {
  51.                         list.Add(conv);
  52.                     }
  53.                 }
  54.             }
  55.             return HandleEngFormat(word, list);
  56.         }
  57.  
  58.         public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, bool> where)
  59.         {
  60.             List<string> list = new List<string>();
  61.             if (typeof(T).IsValueType)
  62.             {
  63.                 string conv;
  64.                 foreach (T item in collection)
  65.                 {
  66.                     if (where(item) && !string.IsNullOrEmpty(conv = item.ToString()))
  67.                     {
  68.                         list.Add(conv);
  69.                     }
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 string conv;
  75.                 foreach (T item in collection)
  76.                 {
  77.                     if (item != null && where(item) && !string.IsNullOrEmpty(conv = item.ToString()))
  78.                     {
  79.                         list.Add(conv);
  80.                     }
  81.                 }
  82.             }
  83.             return HandleEngFormat(word, list);
  84.         }
  85.  
  86.         public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, string> select)
  87.         {
  88.             List<string> list = new List<string>();
  89.             if (typeof(T).IsValueType)
  90.             {
  91.                 string conv;
  92.                 foreach (T item in collection)
  93.                 {
  94.                     if (!string.IsNullOrEmpty(conv = select(item)))
  95.                     {
  96.                         list.Add(conv);
  97.                     }
  98.                 }
  99.             }
  100.             else
  101.             {
  102.                 string conv;
  103.                 foreach (T item in collection)
  104.                 {
  105.                     if (item != null && !string.IsNullOrEmpty(conv = select(item)))
  106.                     {
  107.                         list.Add(conv);
  108.                     }
  109.                 }
  110.             }
  111.             return HandleEngFormat(word, list);
  112.         }
  113.  
  114.         public static string ToEngFormat<T>(this IEnumerable<T> collection, string word, Func<T, bool> where, Func<T, string> select)
  115.         {
  116.             List<string> list = new List<string>();
  117.             if (typeof(T).IsValueType)
  118.             {
  119.                 string conv;
  120.                 foreach (T item in collection)
  121.                 {
  122.                     if (where(item) && !string.IsNullOrEmpty(conv = select(item)))
  123.                     {
  124.                         list.Add(conv);
  125.                     }
  126.                 }
  127.             }
  128.             else
  129.             {
  130.                 string conv;
  131.                 foreach (T item in collection)
  132.                 {
  133.                     if (item != null && where(item) && !string.IsNullOrEmpty(conv = select(item)))
  134.                     {
  135.                         list.Add(conv);
  136.                     }
  137.                 }
  138.             }
  139.             return HandleEngFormat(word, list);
  140.         }
  141.  
  142.         public static string ToEngFormat<T, T2>(this IEnumerable<T> collection, string word, Func<T, T2> select, Func<T2, bool> where)
  143.         {
  144.             T2 ykno;
  145.             List<string> list = new List<string>();
  146.             if (typeof(T).IsValueType)
  147.             {
  148.                 foreach (T item in collection)
  149.                 {
  150.                     ykno = select(item);
  151.                     if (where(ykno))
  152.                     {
  153.                         list.Add(ykno.ToString());
  154.                     }
  155.                 }
  156.             }
  157.             else
  158.             {
  159.                 string conv;
  160.                 foreach (T item in collection)
  161.                 {
  162.                     if (item == null || (ykno = select(item)) == null)
  163.                     {
  164.                         continue;
  165.                     }
  166.                     if (where(ykno) && !string.IsNullOrEmpty(conv = ykno.ToString()))
  167.                     {
  168.                         list.Add(conv);
  169.                     }
  170.                 }
  171.             }
  172.             return HandleEngFormat(word, list);
  173.         }
  174.  
  175.         public static string ToEngFormat<T, T2>(this IEnumerable<T> collection, string word, Func<T, T2> select, Func<T2, bool> where, Func<T2, string> select2)
  176.         {
  177.             T2 ykno;
  178.             string ykno2;
  179.             List<string> list = new List<string>();
  180.             if (typeof(T).IsValueType)
  181.             {
  182.                 foreach (T item in collection)
  183.                 {
  184.                     ykno = select(item);
  185.                     if (where(ykno) && !string.IsNullOrEmpty(ykno2 = select2(ykno)))
  186.                     {
  187.                         list.Add(ykno2);
  188.                     }
  189.                 }
  190.             }
  191.             else
  192.             {
  193.                 foreach (T item in collection)
  194.                 {
  195.                     if (item == null || (ykno = select(item)) == null)
  196.                     {
  197.                         continue;
  198.                     }
  199.                     if (where(ykno) && !string.IsNullOrEmpty(ykno2 = select2(ykno)))
  200.                     {
  201.                         list.Add(ykno2);
  202.                     }
  203.                 }
  204.             }
  205.             return HandleEngFormat(word, list);
  206.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement