using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; namespace Sarand.Net.Standard.Base.Core { //======================================================================= public class CodeDesc { //------------------------------------------------------------------- // Data members. //------------------------------------------------------------------- public CodeDesc (String code, String desc, String comment = "") { Code = StringUtil.EmptyOnNull(code); Desc = StringUtil.EmptyOnNull(desc); Comment = StringUtil.EmptyOnNull(comment); } //------------------------------------------------------------------- // Properties. public String Code { get; } public String Desc { get; } public String Comment { get; } //------------------------------------------------------------------- } //======================================================================= public static class CodeDescList { //------------------------------------------------------------------- // Population. public static void Add (this List values, String code, string desc, String comment = "") { values.Add(new CodeDesc(code, desc, comment)); } public static void Insert (this List values, int idx, String code, string desc, String comment = "") { values.Insert (idx, new CodeDesc(code, desc, comment)); } //------------------------------------------------------------------- // Sorting. public static void SortByCode (this List values) { values.Sort( delegate (CodeDesc codeDesc1, CodeDesc codeDesc2) { return codeDesc1.Code.CompareTo(codeDesc2.Code); } ); } public static void SortByDesc (this List values) { values.Sort( delegate (CodeDesc codeDesc1, CodeDesc codeDesc2) { return codeDesc1.Desc.CompareTo(codeDesc2.Desc); } ); } //------------------------------------------------------------------- // Conversion. public static List GetCodeList (this List values) { List codes = new List(); foreach (var value in values) if (!codes.Contains(value.Code)) codes.Add(value.Code); return codes; } public static List GetDescList (this List values) { List descs = new List(); foreach (var value in values) if (!descs.Contains(value.Desc)) descs.Add(value.Desc); return descs; } //------------------------------------------------------------------- // Lookup. public static bool FindCode (this List values, String code) { return values.Exists(item => item.Code.EqualsCI(code)); } public static CodeDesc GetByCode (this List values, String code) { return values.Find(item => item.Code.EqualsCI(code)); } public static CodeDesc GetByNumericCode (this List values, String code) { if (!StringUtil.IsNumeric(code)) return null; return values.Find(item => StringUtil.IsNumeric(item.Code) && StringUtil.ToInt(item.Code) == StringUtil.ToInt(code)); } //------------------------------------------------------------------- // Lookup code. /// Return code matching supplied code (case-insenstive) - to ensure case matches list. public static String GetCodeForCode (this List values, String code, String defaultValue = "") { foreach (var value in values) if (value.Code.EqualsCI(code)) return value.Code; return StringUtil.EmptyOnNull(defaultValue); } /// Return code matching supplied description (case-insenstive). public static String GetCodeForDesc (this List values, String desc, String defaultValue = "") { foreach (var value in values) if (value.Desc.EqualsCI(desc)) return value.Code; return StringUtil.EmptyOnNull(defaultValue); } //------------------------------------------------------------------- // Lookup desc. public static String GetDescForCode (this List values, String code, String defaultValue = "") { foreach (var value in values) if (value.Code.EqualsCI(code)) return value.Desc; return StringUtil.EmptyOnNull(defaultValue); } //------------------------------------------------------------------- } //======================================================================= }