C# Nützliche Extensions

 
Bewertung:  / 0
SchwachSuper 

Wochenanfang ermitteln

        public static DateTime GetStartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0) diff += 7;
            return dt.AddDays(-1 * diff).Date;
        }

Wochenende ermitteln

        public static DateTime GetEndOfWeek(this DateTime dt, DayOfWeek endOfWeek)
        {
            int diff = endOfWeek - dt.DayOfWeek;
            if (diff < 0) diff += 7;
            return dt.AddDays(diff + 1).Date;
        }

Datumsvergleich

        public static bool EqualDateTime(this DateTime DT1, DateTime DT2)
        {
            DT1 = new DateTime(DT1.Year, DT1.Month, DT1.Day, DT1.Hour, DT1.Minute, DT1.Second);
            DT2 = new DateTime(DT2.Year, DT2.Month, DT2.Day, DT2.Hour, DT2.Minute, DT2.Second);
            if (DT1 == DT2) return true;
            else return false;
        }

 String zu Bool

        public static bool ToBool(this string s)
        {
            if (string.IsNullOrEmpty(s)) return false;
            s = s.Trim();
            if (s.Equals("TRUE", StringComparison.InvariantCultureIgnoreCase) || s.Equals("Y", StringComparison.InvariantCultureIgnoreCase) || s.Equals("on", StringComparison.InvariantCultureIgnoreCase)) return true;
            return false;
        }

String zu Int

        public static int ToInt(this string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;
            int ival = 0;
            int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out ival);
            return ival;
        }

String zu Double

        public static double ToDouble(this string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;
            if (s.Contains(",") && !s.Contains("."))
            {
                s = s.Replace(',', '.');
            }
            double ival = 0;
            double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out ival);
            return ival;
        }

String zu Array

        public static string[] ToArray(this string s)
        {
            if (string.IsNullOrEmpty(s)) return new string[] { };
            return s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        }

Enumeration zu SQL IN

        public static string ToSQLArrayString<T>(this IEnumerable<T> collection, bool addQuotes)
        {
            return ToSQLArrayString<T>(collection, addQuotes, ',');
        }

Enumeration zu JSON

        public static string ToJSON<T>(this IEnumerable<T> collection)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(IEnumerable<T>));
            MemoryStream ms = new MemoryStream();
            XmlDictionaryWriter writer = JsonReaderWriterFactory.CreateJsonWriter(ms, Encoding.UTF8);
            json.WriteObject(ms, collection);
            writer.Close();
            string sJSON = Encoding.UTF8.GetString(ms.GetBuffer());
            ms.Close();
            return sJSON;
        }

 Datum zu Kalenderwoche

        public static int ToCalendarWeek(this DateTime dt)
        {
            CultureInfo CUI = CultureInfo.CurrentCulture;
            return CUI.Calendar.GetWeekOfYear(dt, CUI.DateTimeFormat.CalendarWeekRule, CUI.DateTimeFormat.FirstDayOfWeek);
        }