public static DigestEmailView ToDigestEmailView(DigestEmail email)
        {
            DigestEmailView view = new DigestEmailView();

            if (email != null)
            {
                view.Id = email.Id;
                view.Email = email.Email;
                view.Lines = email.DigestEmailLines.Select(o => o.Line).ToList();
                view.IsDaily = email.IsDaily;
                view.IsWeekly = email.IsWeekly;
            }

            return view;
        }
        public static bool Update(DigestEmailView view)
        {
            using (DB db = new DB(DBHelper.GetConnectionString()))
            {
                DigestEmail email = (from o in db.DigestEmails
                                     where o.Id == view.Id
                                     select o).FirstOrDefault();

                if (email != null)
                {
                    email.Email = view.Email;
                    email.IsDaily = view.IsDaily;
                    email.IsWeekly = view.IsWeekly;

                    AddLines(email.Id, view.Lines);

                    return db.SaveChanges() > 0;
                }

            }

            return false;
        }
        public static DigestEmail ToDigestEmail(DigestEmailView view)
        {
            DigestEmail email = new DigestEmail();

            if (view != null)
            {
                email.Id = view.Id;
                email.Email = view.Email;
                email.IsDaily = view.IsDaily;
                email.IsWeekly = view.IsWeekly;
            }
            return email;
        }
        public static DigestEmailView Insert(DigestEmailView view)
        {
            using (DB db = new DB(DBHelper.GetConnectionString()))
            {
                DigestEmail email = new DigestEmail();

                if (email != null)
                {
                    email.Email = view.Email;
                    email.Client = view.Client;
                    email.IsWeekly = view.IsWeekly;
                    email.IsDaily = view.IsDaily;

                    db.AddToDigestEmails(email);

                    db.SaveChanges();

                    if(email.Id > 0)
                        AddLines(email.Id, view.Lines);

                    return ToDigestEmailView(email);
                }

            }

            return view;
        }
        public static DigestEmailView GetEmailView(int id)
        {
            DigestEmail email = GetEmail(id);

            if (!email.DigestEmailLines.IsLoaded)
                email.DigestEmailLines.Load();

            DigestEmailView view = new DigestEmailView();

            if (email != null)
            {
                view = ToDigestEmailView(email);
            }

            return view;
        }
        private DigestEmailView Deserialize(string data)
        {
            try
            {
                var jss = new JavaScriptSerializer();

                DigestEmailView view = new DigestEmailView();

                view = jss.Deserialize<DigestEmailView>(data);

                return view;
            }
            catch (Exception)
            {
                return null;
            }
        }
        protected object ToObject(DigestEmailView view)
        {
            if (view != null)
            {
                return new
                {
                    Id = view.Id,
                    Email = view.Email,
                    Lines = view.Lines,
                    IsDaily = view.IsDaily,
                    IsWeekly = view.IsWeekly
                };
            }

            return null;
        }