Example #1
0
        public static UnitCollection CreateInstance(IEnumerable <IUnit> units = null)
        {
            if (units == null)
            {
                units = UnitCollection.CreateUnits();
            }

            return(new UnitCollection(units));
        }
Example #2
0
        /// <summary>
        /// Returns a collection of units that match the string.
        /// For example: "Meter" returns the unit whose name is Meter.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static IEnumerable <IUnit> GetMatchingUnits(string s)
        {
            var units = UnitCollection.CreateUnits();

            s = s.Trim();

            // check if the unit name matches exactly

            var nameMatchesExactly = units.Where(p => p.ID.ToString().Equals(s, StringComparison.CurrentCultureIgnoreCase) ||
                                                 p.Name.Equals(s, StringComparison.CurrentCultureIgnoreCase) ||
                                                 p.GetPlural().Equals(s, StringComparison.CurrentCultureIgnoreCase));

            if (nameMatchesExactly.Any())
            {
                return(nameMatchesExactly);
            }

            // check if the unit symbol matches exactly

            var symbolMatchesExactly = units.Where(p => p.Symbol.TrimStart('°').Equals(s, StringComparison.CurrentCultureIgnoreCase));

            if (symbolMatchesExactly.Any())
            {
                return(symbolMatchesExactly);
            }

            // check if there are unit names that start with the string

            var namesStartWith = units.Where(p => p.Name.StartsWith(s, StringComparison.CurrentCultureIgnoreCase));

            if (namesStartWith.Any())
            {
                return(namesStartWith);
            }

            return(null);
        }