Exemple #1
0
        /// <summary>
        /// Loops through the values in an enum type and gets the ids and names
        /// for use in the generated lookup table.
        /// </summary>
        /// <param name="lookup">Enum to process</param>
        /// <param name="getDescriptions">Descriptions of enumeration members should be got out of <see cref="DescriptionAttribute"/> got, because it will be used for DB description column.</param>
        /// <exception cref="System.ArgumentException">Lookup type must be an enum;lookup</exception>
        public ICollection <LookupValue> GetLookupValues(Type lookup, bool getDescriptions = false)
        {
            if (!lookup.IsEnum)
            {
                throw new ArgumentException("Lookup type must be an enum", "lookup");
            }

            var namesWithDescription = !getDescriptions;

            var values = new List <LookupValue>();

            foreach (Enum value in Enum.GetValues(lookup))
            {
                if (IsRuntimeOnly(value))
                {
                    continue;
                }

                // avoid cast error for byte enums by converting to int before using a cast
                // https://github.com/timabell/ef-enum-to-lookup/issues/20
                var numericValue = Convert.ChangeType(value, typeof(int));

                var lookupValue = new LookupValue
                {
                    Id   = (int)numericValue,
                    Name = EnumName(value, !getDescriptions),
                };

                if (getDescriptions)
                {
                    lookupValue.Description = EnumParser.EnumDescriptionValue(value);
                }

                values.Add(lookupValue);
            }
            return(values);
        }
 private static void AppendSelectValueWithDescription(StringBuilder sb, LookupValue lookupValue)
 {
     sb.Append($"SELECT {lookupValue.Id}, N''{lookupValue.Name.SanitizeExecuteSqlString()}'', {GetDescription(lookupValue)}");
 }
 private static void AppendSelectValues(StringBuilder sb, LookupValue lookupValue)
 {
     sb.Append($"SELECT {lookupValue.Id}, N''{lookupValue.Name.SanitizeExecuteSqlString()}''");
 }
 private static void AppendValueTupleWithDescription(StringBuilder sb, LookupValue lookupValue)
 {
     sb.Append($"({lookupValue.Id}, N''{lookupValue.Name.SanitizeExecuteSqlString()}'', {GetDescription(lookupValue)})");
 }
 private static void AppendValueTuple(StringBuilder sb, LookupValue lookupValue)
 {
     sb.Append($"({lookupValue.Id}, N''{lookupValue.Name.SanitizeExecuteSqlString()}'')");
 }
        private static string GetDescription(LookupValue lookupValue)
        {
            string description = lookupValue.Description == null ? "CAST(null as nvarchar(max))" : "N''" + lookupValue.Description.SanitizeExecuteSqlString() + "''";

            return(description);
        }