48 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VEPROMS.CSLA.Library
{
public static class EnumExtensions
{
// Take a flag enum and extract the cleaned string values.
public static List<string> ToComparableStrings(this Enum eNum)
=> eNum.ToString()
.Split(',')
.Select(str => str.ToCleanString())
.ToList();
// Take an individual enum and report the textual value.
public static string ToComparableString(this Enum eNum)
=> eNum.ToString()
.ToCleanString();
// Take an individual enum and report the integer values as a comma delimited string
public static string ToCommaDelimString(this Enum eNum)
{
string retVal = string.Empty;
if (!string.IsNullOrEmpty(eNum.ToString()))
{
foreach (Enum f in Enum.GetValues(eNum.GetType()))
{
if (eNum.ToString().Contains(f.ToString()))
{
retVal = (string.IsNullOrEmpty(retVal)) ? Convert.ToInt32(f).ToString() : string.Concat(retVal, ",", Convert.ToInt32(f));
}
}
}
return retVal;
}
// Remove any spaces due to split and if `_` found change it to space.
public static string ToCleanString(this string str)
=> str.Replace(" ", string.Empty)
.Replace('_', ' ');
}
}