public void Insert_Volunteer_Service_period(Volunteer_Schedule_saveModel[] SM)
 {
     using (VolunteerEntities db = new VolunteerEntities())
     {
         for (int i = 0; i < SM.Length; i += 1)
         {
             var stage_type = SM[i].GStage;
             if (stage_type != "續留")
             {
                 var stage = (from s in db.Stages
                              where s.Stage1 == stage_type && s.Stage_type == "排班意願"
                              select s.Stage_ID).ToList();
                 Service_period2 sm = new Service_period2()
                 {
                     Volunteer_no      = SM[i].Volunteer_no,
                     Service_period_no = SM[i].Service_period_no,
                     Srevice_group     = SM[i].Srevice_group,
                     Application_unit  = SM[i].Application_unit,
                     Stage             = stage[0]
                 };
                 db.Service_period2.Add(sm);
             }
         }
         db.SaveChanges();
     }
 }
        public void Inertgroup_andexpertise(List <int> AD)
        {
            var Sign_up_list = (from n in Myentity.Sign_up
                                select n).ToList();
            var Sign_up_expertise_list = (from n in Myentity.Sign_up_expertise
                                          select n).ToList();

            var Sign_up_Service_period_list = (from n in Myentity.Sign_up_Service_period select n).ToList();

            var account_list = (from n in Myentity.accounts select n).ToList();

            var stage_id = (from n in Myentity.Stages where n.Stage1 == "未排班" && n.Stage_type == "排班意願" select n.Stage_ID).First();

            foreach (var row in AD)
            {
                Sign_up sign_Up = Sign_up_list.Where(p => p.Sign_up_no == row).First();

                var q = (from n in Myentity.Volunteer
                         where n.Chinese_name == sign_Up.Chinese_name &&
                         n.Join_date == sign_Up.Sign_up_date
                         select n).First();

                Service_Group1 service_Group1 = new Service_Group1();
                service_Group1.Volunteer_no = q.Volunteer_no;
                service_Group1.Group_no     = 1;

                Myentity.Service_Group1.Add(service_Group1);

                var expertise_list = Sign_up_expertise_list.Where(p => p.Sign_up_no == row).ToList();
                foreach (var expertise in expertise_list)
                {
                    Expertise2 expertise2 = new Expertise2();
                    expertise2.Volunteer_no = q.Volunteer_no;
                    expertise2.Expertise_no = (int)expertise.Expertise;

                    Myentity.Expertise2.Add(expertise2);
                }

                var Service_period_list = Sign_up_Service_period_list.Where(p => p.Sign_up_no == row).ToList();
                foreach (var _period in Service_period_list)
                {
                    Service_period2 service_Period2 = new Service_period2();
                    service_Period2.Volunteer_no      = q.Volunteer_no;
                    service_Period2.Service_period_no = _period.Service_period_no;
                    service_Period2.Stage             = stage_id;
                    service_Period2.Wish_order        = _period.Wish_order;

                    Myentity.Service_period2.Add(service_Period2);
                }

                var     account  = account_list.Select(p => p.Account_number).Max().Trim();
                account _account = new account();
                _account.Account_number = (int.Parse(account) + 1).ToString();
                _account.Password       = (int.Parse(account) + 1).ToString();
                _account.User_ID        = q.Volunteer_no;
                _account.Permission     = "Volunteer";

                Myentity.accounts.Add(_account);

                ThreadStart TS = new ThreadStart(
                    delegate()
                {
                    sending_email(sign_Up.Email, sign_Up.Chinese_name + sign_Up.English_name, Issend_pass, _account.Account_number);                    //寄送郵件
                    System.Windows.Forms.Application.DoEvents();
                });
                send_mail = new Thread(TS);
                send_mail.IsBackground = true;
                send_mail.Start();
                break;
            }

            this.Myentity.SaveChanges();
        }