/// <summary> /// test if Energiemenge has only one <see cref="Mengeneinheit"/> /// </summary> /// <param name="em">Energiemenge</param> /// <returns>true iff the Energiemenge->energieverbrauch list does only contain entries with mutually convertible units</returns> public static bool IsPureMengeneinheit(this BO4E.BO.Energiemenge em) { using (MiniProfiler.Current.Step(nameof(IsPureMengeneinheit))) { ISet <Mengeneinheit> einheiten = new HashSet <Mengeneinheit>(); em.Energieverbrauch.All <Verbrauch>(v => einheiten.Add(v.Einheit)); if (einheiten.Count <= 1) { return(true); } else { Mengeneinheit me1 = einheiten.First <Mengeneinheit>(); foreach (Mengeneinheit me2 in einheiten) { if (!me1.IsConvertibleTo(me2)) { return(false); } } return(true); } } }
/// <summary> /// returns the factor that is needed to convert an amount in unit <paramref name="me1" /> to an equivalent amount in /// unit <paramref name="me2" />. /// </summary> /// <param name="me1">source unit</param> /// <param name="me2">target unit</param> /// <returns></returns> /// <exception cref="InvalidOperationException">iff units do not have the same dimension</exception> public static decimal GetConversionFactor(this Mengeneinheit me1, Mengeneinheit me2) { #pragma warning disable 618 if (me1 == Mengeneinheit.ZERO || me2 == Mengeneinheit.ZERO) #pragma warning restore 618 { throw new InvalidOperationException("You must not use the artificial 'ZERO' value."); } if (me1 == me2) { return(1.0M); } if (!me1.IsConvertibleTo(me2)) { throw new InvalidOperationException( $"{me1} and {me2} are not convertible into each other because they don't share the same dimension."); } if ((int)me1 % (int)me2 == 0 || (int)me2 % (int)me2 == 0) { return((decimal)me1 / (decimal)me2); } throw new InvalidOperationException($"{me1} and {me2} are not (trivially) convertible into each other."); }
/// <summary> /// returns the factor that is needed to convert an amount in unit <paramref name="me1"/> to an equivalent amount in unit <paramref name="me2"/>. /// </summary> /// <param name="me1">source unit</param> /// <param name="me2">target unit</param> /// <returns></returns> /// <exception cref="InvalidOperationException">iff units do not have the same dimension</exception> public static decimal GetConversionFactor(this Mengeneinheit me1, Mengeneinheit me2) { if (me1 == me2) { return(1.0M); } if (!me1.IsConvertibleTo(me2)) { throw new InvalidOperationException($"{me1} and {me2} are not convertible into each other because they don't share the same dimension."); } if ((int)me1 % (int)me2 == 0 || (int)me2 % (int)me2 == 0) { return((decimal)me1 / (decimal)me2); } else { throw new InvalidOperationException($"{me1} and {me2} are not (trivially) convertible into each other."); } }