public decimal updateLiveOkoo()
        {
            DataTable dt = OkooHtml.GetTableOutHtml();
            using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
            {
                var ll = match.Live_okoo.Where(e => e.Live_okoo_id > 0);
                match.Live_okoo.DeleteAllOnSubmit(ll);//更新后删除
                match.SubmitChanges();

                foreach (DataRow aa in dt.Rows)
                {
                    if (HtmlTextToStr(aa[15].ToString()) != null)
                    {
                        Live_okoo lo = new Live_okoo();
                        lo.KeyValue = HtmlValueToInt(aa[0].ToString());
                        lo.LeagueName = HtmlTextToStr(aa[1].ToString());
                        lo.Match_time = HtmlTextToStr(aa[2].ToString());
                        lo.MatchOrder1_HomeName = TeamName(aa[3].ToString());
                        lo.MatchOrder1_HandicapNumber = HtmlTextToStr(aa[4].ToString());
                        lo.MatchOrder1_AwayName = TeamName(aa[5].ToString());
                        lo.Ok_1_0 = HtmlTextToStr(aa[6].ToString());
                        lo.Ok_1_1 = HtmlTextToStr(aa[7].ToString());
                        lo.Ok_1_2 = HtmlTextToStr(aa[8].ToString());
                        lo.MatchInfo = HtmlTextToStr(aa[11].ToString());
                        lo.Match_1_Win = HtmlTextToStr(aa[12].ToString());
                        lo.Match_1_Drawn = HtmlTextToStr(aa[13].ToString());
                        lo.Match_1_Lost = HtmlTextToStr(aa[14].ToString());

                        match.Live_okoo.InsertOnSubmit(lo);
                        match.SubmitChanges();
                    }
                }
                return match.Live_okoo.Max(e => e.Live_okoo_id);
            }
        }
 private void init_table_id_value()
 {
     using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
     {
         var uri = match.Match_table_xpath.Where(e => e.Uri_host == uri_host).FirstOrDefault();
         if (uri.Max_table_id_value.Length > 1)
         {
             table_id_value = uri.Max_table_id_value;
             tablexpath = uri.Max_table_xpath;
         }
         else
         {
             if (uri.Second_table_id_value.Length > 1)
             {
                 table_id_value = uri.Second_table_id_value;
                 tablexpath = uri.Second_table_xpath;
             }
             else
             {
                 table_id_value = uri.Max_table_xpath;
                 tablexpath = uri.Max_table_xpath;
             }
         }
     }
 }
        public decimal updateLiveAibo()
        {
            DataTable dt = AiboHtml.GetTableOutHtml();
            using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
            {
                var ll = match.Live_Aibo.Where(e => e.Live_Aibo_id > 0);
                match.Live_Aibo.DeleteAllOnSubmit(ll);//更新后删除
                match.SubmitChanges();

                foreach (DataRow aa in dt.Rows)
                {
                    if (HtmlTextToStr(aa[3].ToString()) != null) // && HtmlTextToStr(aa[11].ToString()) == null
                    {
                        Live_Aibo lo = new Live_Aibo();
                        lo.KeyValue = HtmlTextToStr(aa[1].ToString());
                        lo.LeagueName = HtmlTextToStr(aa[2].ToString());
                        lo.Match_time = HtmlTextToStr(aa[3].ToString());
                        lo.MatchOrder1_HomeName = AiboTeamName(aa[5].ToString());
                        lo.MatchOrder1_HandicapNumber = HtmlTextToStr(aa[6].ToString());
                        lo.MatchOrder1_AwayName = AiboTeamName(aa[7].ToString());

                        match.Live_Aibo.InsertOnSubmit(lo);
                       
                    }
                }
                match.SubmitChanges();
                return match.Live_Aibo.Max(e => e.Live_Aibo_id);
            }
        }
 public decimal updateMacauslot()
 {
     DataTable dt = MacauslotHtml.GetTableInnerText();
     DateTime dtt = DateTime.Now.Date;
     using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
     {
         foreach (DataRow aa in dt.Rows)
         {
             if (aa[0].ToString().IndexOf("★") != -1)
                 if (aa[4].ToString() != null)
                     if (aa[4].ToString().Length > 1)
                     {
                         MacauPredication mp = new MacauPredication();
                         mp.Record_datetime = dtt;
                         mp.Home_team = extract(aa[1].ToString());
                         mp.Away_team = extract(aa[5].ToString());
                         mp.Macauslot = aa[0].ToString().Trim();
                         mp.Predication = aa[4].ToString();
                         match.MacauPredication.InsertOnSubmit(mp);
                     }
         }
         match.SubmitChanges();
         return match.MacauPredication.Max(e => e.MacauPredication_id);
     }
 }
        public decimal InsertLastHtmlTableToDB()
        {
            DataTable dt = SevenmResult.GetHtmlTable();

            DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn);

            foreach (DataRow aa in dt.Rows)
            {
                if (HtmlTextToStr(aa[3].ToString()) != null)
                {
                    Result_tb rt = new Result_tb();
                    rt.Html_position = aa[0].ToString();
                    rt.Match_type = HtmlTextToStr(aa[1].ToString());
                    rt.S_time = HtmlTextToStr(aa[2].ToString());
                    rt.Home_team = HtmlTextToStr(aa[3].ToString());
                    rt.Full_time_score = HtmlTextToStr(aa[4].ToString());
                    rt.Away_team = HtmlTextToStr(aa[5].ToString());
                    rt.Half_time_score = HtmlTextToStr(aa[6].ToString());
                    rt.Odds = HtmlTextToStr(aa[7].ToString());
                    rt.Win_loss_big = HtmlTextToStr(aa[8].ToString());
                    rt.S_date = HtmlDateToStrResult(aa[2].ToString());
                    rt.Home_team_big = HtmlHrefToStr(aa[3].ToString());
                    rt.Away_team_big = HtmlHrefToStr(aa[5].ToString());
                    match.Result_tb.InsertOnSubmit(rt);
                }
            }
            match.SubmitChanges();

            return match.Result_tb.Select(e => e.Result_tb_id).Max();
        }
        private void Form1_Load(object sender, EventArgs ee)
        {


            注册LicenseCheck();
            dataGridView5.Visible = false;
            toolStripStatusLabel2.Text = dateTimePicker2.Value.ToString("yyyy-MM-dd");//日历组建日期字符串格式化方法
            DateTime? dt, dt1;
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                dt = matches.Result_tb_lib.Max(p => p.Match_time);
                if (dt != null)
                    toolStripStatusLabel3.Text = dt.ToString();
                else
                    toolStripStatusLabel3.Text = "2002.1.1";

                dt1 = matches.Live_Table_lib.Max(p => p.Match_time);
                toolStripStatusLabel4.Text = dt1.HasValue ? dt1.Value.ToString() : null;
            }
            treeView5.Nodes.Clear();
            loaddatatree.TreeViewMatch(treeView5, "type");
            dateTimePicker1.Value = DateTime.Parse(toolStripStatusLabel3.Text);
            textBox2.Text = "1";

        }
        public void top20Algorithm()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                int i = 0;
                foreach (var id in idExc)
                {
                    i++;
                    ProgressBarDelegate.DoSendPMessage(i);
                    Application.DoEvents();
                    RowNumberLimit r = new RowNumberLimit(id);
                    r.initCurveFit();
                    Match_analysis_result mar = new Match_analysis_result();
                    mar.Live_table_lib_id = r.live_id;
                    mar.Pre_algorithm = "top20";
                    mar.Pre_match_count = r.Top20Count;
                    mar.Home_goals = r.HomeGoals;
                    mar.Away_goals = r.AwayGoals;
                    mar.Home_w = r.hWin;
                    mar.Home_d = r.hDraw;
                    mar.Home_l = r.hLose;
                    mar.Fit_win_loss = r.CureFitWinLoss();
                    mar.Fit_goals = r.CureFitGoals();
                    mar.Fit_odd_even = r.CureFitOddEven();
                    matches.Match_analysis_result.InsertOnSubmit(mar);

                    //2011.10.8  销毁
                    r.Dispose();

                }
                matches.SubmitChanges();
            }
        }
        public int InsertLiveHtmlTableToDB()
        {
            DataTable dt = SevenmLive.GetHtmlTable();

            DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn);

            if (!Conn.CreateTable(typeof(Live_Table))) { return 0; }
            //var result = match.Live_Table.Where(r => r.Live_table_id > 0);
            //match.Live_Table.DeleteAllOnSubmit(result);
            //match.SubmitChanges();

            string ddate = null;
            string ndate = null; //修改当天完场的问题  2011.6.11
            Queue<string> todayDate = new Queue<string>();

            foreach (DataRow aa in dt.Rows)
            {
                if (HtmlDateToStrLive(aa[1].ToString()) != null)
                {
                    if (HtmlDateToStrLive(aa[1].ToString()).IndexOf("最新賽果") == -1)
                    {
                        ddate = HtmlDateToStrLive(aa[1].ToString());
                        ddate = ddate.Replace("年", "-").Replace("月", "-").Replace("日", " ");
                        todayDate.Enqueue(ddate);
                    }
                    else
                    {
                        todayDate.Dequeue();//删除第1条  2011.6.11 
                        ndate = todayDate.Peek();//取第1个元素  2011.6.11
                        ddate = ndate;
                    }
                }

                if (HtmlTextToStr(aa[3].ToString()) != null)
                {
                    Live_Table lt = new Live_Table();
                    lt.Html_position = aa[0].ToString();
                    lt.Match_type = HtmlTextToStr(aa[2].ToString());
                    lt.S_time = HtmlTextToStr(aa[3].ToString());
                    lt.Home_team = HtmlTextToStr(aa[5].ToString());
                    lt.Full_time_score = HtmlTextToStr(aa[6].ToString());
                    lt.Away_team = HtmlTextToStr(aa[7].ToString());
                    lt.Half_time_score = HtmlTextToStr(aa[8].ToString());
                    lt.S_date = ddate;
                    lt.Home_team_big = HtmlHrefToStr(aa[5].ToString());
                    lt.Away_team_big = HtmlHrefToStr(aa[7].ToString());

                    match.Live_Table.InsertOnSubmit(lt);


                }
            }
            match.SubmitChanges();
            return match.Live_Table.Max(e => e.Live_table_id);
        }
 private void button16_Click(object sender, EventArgs c)
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         var idExc = matches.Live_Table_lib
            .Where(e => e.Match_time.Value.Date >= DateTime.Now.AddDays(ViewMatchOverDays).Date)
            .Select(e => e.Match_type).Distinct();
         foreach (string matchtype in idExc)
             treeView6.Nodes.Add(matchtype);
     }
 }
        private void SimulinkGRNN()
        {
            try
            {
                string result = null;
                ExportToExcel.DataGridView2Txt(dataGridView2, nn.tempy, 5);
                ExportToExcel.DataGridView2Txt(dataGridView3, nn.tempx, 6);


                //exe文件方式
                //result = ExportToExcel.SimulinkNN(@"D:\My Documents\MATLAB\mygrnn.exe");

                //dll文件方式
                result = nn.NewGrnn();

                richTextBox3.Text = result;

                //dataGridView3.Columns.Add("...", "...");
                dataGridView3.Columns.Add("MyGRNN", "MyGRNN");

                int colx = dataGridView3.Columns.Count - 1;
                int ix = 0;
                string[] lines = result.Split(new char[] { '\r', '\n' });
                foreach (string line in lines)
                    if (line != null)
                        if (line.Trim().Length > 0)
                            if (line.IndexOf("=") == -1)
                            {
                                dataGridView3.Rows[ix].Cells[colx].Value = line; ix++;
                            }


                using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
                {
                    int resultid = 0;
                    int col = dataGridView3.Columns.Count - 1;
                    string grnnfit = null;
                    for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
                    {
                        resultid = Int32.Parse(dataGridView3.Rows[i].Cells[0].Value.ToString());
                        grnnfit = dataGridView3.Rows[i].Cells[col].Value.ToString();
                        var mar = matches.Match_analysis_result
                            .Where(e => e.Analysis_result_id == resultid).First();//查找需要更新的数据
                        mar.Grnn_fit = grnnfit.Trim();
                    }
                    matches.SubmitChanges();
                }
                //MessageBox.Show("OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public ForecastAlgorithm()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                var idLive = matches.Live_Table_lib.Select(e => e.Live_table_lib_id).ToList();//立即加载效率高?
                var idAnalysis = matches.Match_analysis_result.Select(e => e.Live_table_lib_id ?? 0).ToList();//可空类型转换
                idExc = idLive.Except(idAnalysis).ToList();   //except序列A有的元素序列B没有
            }

            dMatch.dNew = false;
            dMatch.LoadMatchData(true);
        }
        //private  TreeNode _treeViewMatch;

        //DataClassesMatchDataContext2 dcmdc2 = new DataClassesMatchDataContext2(Conn.conn);

        public LoadAnalysisReview()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                marAll = matches.Match_analysis_result
                    .Where(e => e.Pre_algorithm != "top20").ToLookup(e => e.Live_table_lib_id);
                ltlAll = matches.Live_Table_lib.ToDictionary(e => e.Live_table_lib_id);
                lsAll = matches.Live_Single.ToLookup(e => e.Home_team_big + "-" + e.Away_team_big);
                mpAll = matches.MacauPredication.OrderByDescending(e => e.MacauPredication_id)
                    .Where(e => e.Home_team != null && e.Away_team != null)
                    .ToLookup(e => e.Home_team);
                rtlAll = matches.Result_tb_lib.ToDictionary(e => e.Result_tb_lib_id);
            }
        }
        public AuditForecastAlgorithm(int daysDiff)
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                idExc = matches.Live_Table_lib
                   .Where(e => e.Match_time.Value.Date >= DateTime.Now.AddDays(daysDiff).Date)
                   .Select(e => e.Live_table_lib_id)
                   .ToList();
                //mars = matches.Match_analysis_result;   //这里不能放入list,否则更新不了数据, 2011.7.27
                lss = matches.Live_Single.ToLookup(e => e.Home_team_big + e.Away_team_big);
            }

            dMatch.dNew = false;
            dMatch.LoadMatchData(true);
        }
        public void InitMatchOver()
        {
            if (MatlabMatch.matchover == null)
            {
                using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
                {
                    var matchover = from p in matches.Match_analysis_result
                                    join q in matches.Result_tb_lib on p.Result_tb_lib_id equals q.Result_tb_lib_id
                                    join t in matches.Live_Table_lib on p.Live_table_lib_id equals t.Live_table_lib_id
                                    select new
                                    {
                                        t.Match_time,
                                        t.Match_type,
                                        t.Home_team,
                                        t.Away_team,
                                        q.Odds,
                                        p.Home_w,
                                        p.Home_d,
                                        p.Home_l,

                                        p.Home_goals,
                                        p.Away_goals,

                                        p.Cross_goals,

                                        p.Fit_win_loss,

                                        p.Recent_scores,
                                        p.Recent_2scores,
                                        p.Recent_3scores,
                                        p.Recent_4scores,
                                        p.Recent_5scores,
                                        p.Recent_6scores,

                                        Lottery_Ticket = q.Full_home_goals - q.Full_away_goals,
                                        q.Full_home_goals,
                                        q.Full_away_goals,
                                        Dodds = ConvertOdd(t.Home_team, q.Odds)
                                    };

                    //避免干扰  Where(e=>e.Home_w+e.Home_d+e.Home_l>2)
                    MatlabMatch.matchover = matchover.Where(e=>e.Home_w+e.Home_d+e.Home_l>2).ToDataTable();

                }

            }
        }
        public int InsertLiveHtmlTableToDB(string _html)
        {
            SqlAgilityPackTableXpath tbTag = new SqlAgilityPackTableXpath("live.win.7m.cn");
            HtmlAgilityPackGeneric SevenmLivesg = new HtmlAgilityPackGeneric(_html, tbTag.tbTag, 0);//容易出错,主要路径的转换
            DataTable dt = SevenmLivesg.GetTableOutHtml(); //容易出错,注意方法的转换
            DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn);
            //if (!Conn.CreateTable(typeof(Live_Single))) { return 0; }
            foreach (DataRow aa in dt.Rows)
            {
                if (aa[8] != null)
                    if (aa[8].ToString() != "")
                        if (aa[8].ToString().Trim() != "")
                        {
                            status = HtmlTextToStr(aa[8].ToString()).Trim();

                            //北京单场
                            if (status != "让球")
                            {
                                if (status.Length >= 1 && status.Length <= 2)
                                {
                                    Live_Single ls = new Live_Single();
                                    status = HtmlTextToStr(aa[9].ToString()).Trim();
                                    ls.Html_position = GetNumber(HtmlTextToStr(aa[0].ToString()));
                                    ls.Home_team_big = GetNumber(HtmlHrefToStr(aa[4].ToString()));
                                    ls.Away_team_big = GetNumber(HtmlHrefToStr(aa[6].ToString()));
                                    ls.Status = status;
                                    match.Live_Single.InsertOnSubmit(ls);
                                }
                            }

                            //竞猜
                            if (status.IndexOf(".") != -1)
                            {
                                Live_Single ls = new Live_Single();
                                ls.Html_position = GetNumber(HtmlTextToStr(aa[0].ToString()));
                                ls.Home_team_big = GetNumber(HtmlHrefToStr(aa[4].ToString()));
                                ls.Away_team_big = GetNumber(HtmlHrefToStr(aa[6].ToString()));
                                ls.Status = status;
                                match.Live_Single.InsertOnSubmit(ls);
                            }
                        }
            }
            match.SubmitChanges();
            return match.Live_Single.Max(e => e.Live_Single_id);
        }
 public void DeleteRedundancy()
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         var idLive = from p in matches.Match_analysis_result
                      group p by p.Live_table_lib_id into ttt
                      select new { id = ttt.Key, count = ttt.Count() };
         idDelete = idLive.Where(e => e.count > 1).Select(e => e.id).ToList();
         if (idDelete == null) return;
         foreach (int del in idDelete)
         {
             var delid = matches.Live_Table_lib.Where(e => e.Live_table_lib_id == del).FirstOrDefault();
             matches.Live_Table_lib.DeleteOnSubmit(delid);
             var delid2 = matches.Match_analysis_result.Where(e => e.Live_table_lib_id == del).FirstOrDefault();
             matches.Match_analysis_result.DeleteOnSubmit(delid2);
         }
         matches.SubmitChanges();
     }
 }
        public void initTreeNode(int daysDiff, bool ismath, bool bj)
        {
            //这个连接不能放到class中,不然取的还是缓存的数据???????????
            //对象和数据库之间会存在不能更新的问题???????????
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))

            //using (SoccerScoreSqlite matches = new SoccerScoreSqlite(cnn))
            {
                //if (ismath)
                //    ltlAll = matches.Live_Table_lib
                //        .Where(e => matchlist.Contains(e.Match_type))
                //        .Where(m => m.Match_time.Value.Date >= DateTime.Now.AddDays(daysDiff).Date)
                //        .OrderBy(m => m.Match_time).ToList();
                //else
                ltlAll = matches.Live_Table_lib
                    .Where(m => m.Match_time.Value.Date >= DateTime.Now.AddDays(daysDiff).Date)
                    .OrderBy(m => m.Match_time)
                    .ToDictionary(e => e.Live_table_lib_id);
                rtlAll = matches.Result_tb_lib
                    .Where(m => m.Match_time.Value.Date >= DateTime.Now.AddDays(daysDiff).Date)
                    .ToDictionary(e => e.Result_tb_lib_id);

                //loAll = matches.Live_okoo.Where(e => e.Live_okoo_id > 0).ToList();
                //mpAll = matches.MacauPredication.OrderByDescending(e => e.MacauPredication_id).ToList();
                //lsAll = matches.Live_Single.ToList();

                lsAll = matches.Live_Single.ToLookup(e => e.Home_team_big + "-" + e.Away_team_big);
                mpAll = matches.MacauPredication.OrderByDescending(e => e.MacauPredication_id)
                    .Where(e => e.Home_team != null && e.Away_team != null)
                    .ToLookup(e => e.Home_team);

                //处理北京单场
                if (bj)
                    marAll = matches.Match_analysis_result
                        .Where(e => e.Pre_algorithm != "top20")
                        .Where(e => e.Live_table_lib_id > 0).ToLookup(e => e.Live_table_lib_id);
                else
                    marAll = matches.Match_analysis_result.ToLookup(e => e.Live_table_lib_id);
            }
        }
 private void button9_Click_1(object sender, EventArgs c)
 {
     Uri uri = new Uri(textBox3.Text);
     string host = uri.Host;
     using (DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn))
     {
         var eUri = match.Match_table_xpath.Where(e => e.Uri_host == host).FirstOrDefault();
         if (eUri == null)
         {
             Match_table_xpath nUri = new Match_table_xpath();
             nUri.Uri_host = host.ToString();
             nUri.Max_table_xpath = textBox4.Text;
             nUri.Second_table_xpath = textBox6.Text;
             nUri.Max_table_id_value = textBox7.Text;
             nUri.Second_table_id_value = textBox8.Text;
             match.Match_table_xpath.InsertOnSubmit(nUri);
         }
         else
         {
             eUri.Uri_host = host.ToString();
             eUri.Max_table_xpath = textBox4.Text;
             eUri.Second_table_xpath = textBox6.Text;
             eUri.Max_table_id_value = textBox7.Text;
             eUri.Second_table_id_value = textBox8.Text;
             //match.match_table_xpath.InsertOnSubmit(eUri);
         }
         match.SubmitChanges();
     }
     MessageBox.Show("OK");
 }
 private void button30_Click(object sender, EventArgs e)
 {
     DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn);
     gridControl1.DataSource = matches.Result_tb_lib;
 }
        private void toolStripButton_iniLast_Click(object sender, EventArgs e)
        {
            toolStripLabel1.Text = "updateResultTable";

            try
            {
                this.tabControl1.SelectedTab = this.tabPage1;


                if (Conn.CreateTable(typeof(Result_tb)))
                    Conn.CompressCompact();


                #region

                Application.DoEvents();

                datahtml = false;

                WebHistoryDataGet(); //button2.PerformClick();//执行历史网页采集

                while (!datahtml) Application.DoEvents();

                #endregion

                using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
                {
                    int pb = matches.Result_tb.Count();
                    toolStripProgressBar1.Maximum = pb;

                    //MessageBox.Show(pb.ToString());
                    toolStripLabel2.Text = pb.ToString(); Thread.Sleep(1);
                }
                SevenmResultToSql sevenm = new SevenmResultToSql();
                sevenm.UpdateLastMatch();

                //执行分析数据的更新

                UpdateAnalysisResult_today();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
 //获取数据库的数据结构
 private void button6_Click(object sender, EventArgs e)
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         gridControl1.DataSource = matches.Match_table_xpath;
     }
 }
        public void UpdateLastMatch()
        {
            int i = 0;
            DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn);
            var rt = matches.Result_tb.OrderBy(o => o.S_date).ThenBy(p => p.S_time);//用lambda表达式简洁

            //取临时变量监视
            DateTime lib_max_match_time = matches.Result_tb_lib
                .Max(p => p.Match_time).Value.AddDays(-3);

            //取临时表做监视
            List<Result_tb_lib> temp_tb = matches.Result_tb_lib
                .Where(p => p.Match_time > lib_max_match_time).ToList();

            //取临时表插入
            List<Result_tb_lib> update_tb = new List<Result_tb_lib>();

            foreach (var m in rt)
            {
                i++;
                ProgressBarDelegate.DoSendPMessage(i);
                Application.DoEvents();
                if (m.Home_team_big != null)
                {
                    Result_tb_lib rtl = new Result_tb_lib();
                    rtl.Html_position = Int32.Parse(m.Html_position);
                    rtl.Home_team_big = Int32.Parse(GetNumber(m.Home_team_big));
                    rtl.Away_team_big = Int32.Parse(GetNumber(m.Away_team_big));
                    rtl.Match_type = m.Match_type.Trim();
                    last_line = m.S_time.LastIndexOf("\n");
                    temp_time = m.S_time.Substring(last_line, m.S_time.Length - last_line - 1);
                    rtl.Match_time = DateTime.Parse(m.S_date.Substring(0, 10) + " " + temp_time);
                    rtl.Odds = m.Odds.Trim();
                    rtl.Win_loss_big = m.Win_loss_big.Trim();
                    rtl.Home_team = m.Home_team.Trim();
                    rtl.Away_team = m.Away_team.Trim();
                    rtl.Home_red_card = StringCount(m.Home_team, "&nbsp;", 0);
                    rtl.Away_red_card = StringCount(m.Away_team, "&nbsp;", 0);
                    string bf = m.Full_time_score.Replace("&nbsp;", "").Replace("&nbsp;", "");
                    if (m.Full_time_score.IndexOf("-") > 0)
                    {
                        rtl.Full_home_goals = Int32.Parse(bf.Substring(0, bf.IndexOf("-")));
                        rtl.Full_away_goals = Int32.Parse(bf.Substring(bf.IndexOf("-") + 1, bf.Length - bf.IndexOf("-") - 1));
                    }
                    if (m.Half_time_score.IndexOf("-") > 0)
                    {
                        rtl.Half_home_goals = Int32.Parse(m.Half_time_score.Substring(0, m.Half_time_score.IndexOf("-")));
                        rtl.Half_away_goals = Int32.Parse(m.Half_time_score.Substring(m.Half_time_score.IndexOf("-") + 1, m.Half_time_score.Length - m.Half_time_score.IndexOf("-") - 1));
                    }

                    //如果库中文件的日期太小,直接删除
                    if (rtl.Match_time >= lib_max_match_time)
                    {
                        //数据分区,层次化查询
                        var rtExist = from p in temp_tb  //历史库中的临时表
                                      where p.Match_time.Value.Date == rtl.Match_time.Value.Date     //注意这里时间的问题? 2011.8.30
                                      where p.Home_team_big == rtl.Home_team_big
                                      where p.Away_team_big == rtl.Away_team_big
                                      select p;

                        //库中没有记录直接插入
                        if (!rtExist.Any())
                        {
                            temp_tb.Add(rtl);  //历史库中的临时表做更新
                            update_tb.Add(rtl);
                        }
                    }
                    //更新后删除
                    //matches.Result_tb.DeleteOnSubmit(m);
                }
            }
            matches.Result_tb_lib.InsertAllOnSubmit(update_tb);
            matches.SubmitChanges();
            MessageBox.Show("OK");
        }
        private void button24_Click_2(object sender, EventArgs eeee)
        {
            DialogResult result; //Messagebox所属于的类
            result = MessageBox.Show(this, "YesOrNo", "你确定要删除分析库?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)//Messagebox返回的值
            {
                //提取去年的数据
                DateTime dt = DateTime.Now.AddYears(-1);

                List<Live_Table_lib> ltls = new List<Live_Table_lib>();

                using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
                {
                    var result_tb = matches.Result_tb_lib.Where(e => e.Match_time > dt).OrderBy(e => e.Match_time);
                    foreach (var r in result_tb)
                    {
                        Live_Table_lib ltl = new Live_Table_lib();
                        ltl.Match_time = r.Match_time;
                        ltl.Html_position = r.Html_position;
                        ltl.Match_type = r.Match_type;
                        ltl.Home_team_big = r.Home_team_big;
                        ltl.Home_team = r.Home_team;
                        ltl.Away_red_card = r.Away_red_card;
                        ltl.Away_team_big = r.Away_team_big;
                        ltl.Away_team = r.Away_team;
                        ltl.Away_red_card = r.Away_red_card;
                        ltl.Full_home_goals = r.Full_home_goals;
                        ltl.Full_away_goals = r.Full_away_goals;
                        ltl.Half_home_goals = r.Half_home_goals;
                        ltl.Half_away_goals = r.Half_away_goals;
                        ltl.Status = r.Odds;
                        //matches.Live_Table_lib.InsertOnSubmit(ltl);
                        ltls.Add(ltl);
                    }
                    //matches.SubmitChanges();
                }
                using (SqlConnection con = new SqlConnection(Conn.conn))
                {
                    con.Open();
                    using (SqlTransaction tran = con.BeginTransaction())
                    {
                        var newOrders = ltls;
                        SqlBulkCopy bc = new SqlBulkCopy(con,
                            //SqlBulkCopyOptions.CheckConstraints |
                            //SqlBulkCopyOptions.FireTriggers |
                          SqlBulkCopyOptions.KeepNulls, tran);
                        bc.BulkCopyTimeout = 36000;
                        bc.BatchSize = 10000;
                        bc.DestinationTableName = "Live_Table_lib";
                        bc.WriteToServer(newOrders.AsDataReader());
                        tran.Commit();
                    }
                    con.Close();
                }
                MessageBox.Show("OK");
            }
        }
        private List<Result_tb_lib> AuditUpdateLastMatch()
        {
            int i = 0;

            DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn);

            var rt = matches.Result_tb.OrderBy(o => o.S_date).ThenBy(p => p.S_time);//用lambda表达式简洁

            //取临时表插入
            List<Result_tb_lib> update_tb = new List<Result_tb_lib>();

            foreach (var m in rt)
            {
                i++;
                ProgressBarDelegate.DoSendPMessage(i);
                Application.DoEvents();
                if (m.Home_team_big != null)
                {
                    Result_tb_lib rtl = new Result_tb_lib();
                    rtl.Html_position = Int32.Parse(m.Html_position);
                    rtl.Home_team_big = Int32.Parse(GetNumber(m.Home_team_big));
                    rtl.Away_team_big = Int32.Parse(GetNumber(m.Away_team_big));
                    rtl.Match_type = m.Match_type.Trim();
                    last_line = m.S_time.LastIndexOf("\n");
                    temp_time = m.S_time.Substring(last_line, m.S_time.Length - last_line - 1);
                    rtl.Match_time = DateTime.Parse(m.S_date.Substring(0, 10) + " " + temp_time);
                    rtl.Odds = m.Odds.Trim();
                    rtl.Win_loss_big = m.Win_loss_big.Trim();
                    rtl.Home_team = m.Home_team.Trim();
                    rtl.Away_team = m.Away_team.Trim();
                    rtl.Home_red_card = StringCount(m.Home_team, "&nbsp;", 0);
                    rtl.Away_red_card = StringCount(m.Away_team, "&nbsp;", 0);
                    string bf = m.Full_time_score.Replace("&nbsp;", "").Replace("&nbsp;", "");
                    if (m.Full_time_score.IndexOf("-") > 0)
                    {
                        rtl.Full_home_goals = Int32.Parse(bf.Substring(0, bf.IndexOf("-")));
                        rtl.Full_away_goals = Int32.Parse(bf.Substring(bf.IndexOf("-") + 1, bf.Length - bf.IndexOf("-") - 1));
                    }
                    if (m.Half_time_score.IndexOf("-") > 0)
                    {
                        rtl.Half_home_goals = Int32.Parse(m.Half_time_score.Substring(0, m.Half_time_score.IndexOf("-")));
                        rtl.Half_away_goals = Int32.Parse(m.Half_time_score.Substring(m.Half_time_score.IndexOf("-") + 1, m.Half_time_score.Length - m.Half_time_score.IndexOf("-") - 1));
                    }
                    update_tb.Add(rtl);
                }
            }
            return update_tb;
        }
 public void BatchUpdateLastMatch()
 {
     int i = 0;
     DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn);
     var aulm = AuditUpdateLastMatch().ToLookup(e =>
         e.Match_time.Value.Date.ToShortDateString() 
         + "----------" + e.Home_team_big 
         + "----------" + e.Away_team_big);
     var aulmorder = aulm.OrderBy(e => e.Key.Substring(0, 10));
     foreach (var au in aulmorder)
     {
         i++;
         ProgressBarDelegate.DoSendPMessage(i);
         Application.DoEvents();
         matches.Result_tb_lib.InsertOnSubmit(au.First());
     }
     matches.SubmitChanges();
     MessageBox.Show("OK");
 }
 private void button2_Click(object sender, EventArgs e)
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         gridControl1.DataSource = matches.MacauPredication;
     }
 }
 private void button11_Click(object sender, EventArgs e)
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         gridControl1.DataSource = matches.Live_Single;
     }
 }
 //此项目不用?????
 //批量导入 Result_tb_lib数据,并且做修正  ?
 private void BatchUpdateLastMatch()
 {
     using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
     {
         int pb = matches.Result_tb.Count();
         toolStripProgressBar1.Maximum = pb;
         MessageBox.Show(pb.ToString());
     }
     SevenmResultToSql sevenm = new SevenmResultToSql();
     sevenm.BatchUpdateLastMatch();
 }
        public void top20Algorithm()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                int i = 0;
                foreach (int liveid in idExc)
                {
                    i++;
                    ProgressBarDelegate.DoSendPMessage(i);
                    Application.DoEvents();
                    r = new RowNumberLimit(liveid);
                    r.initCurveFit();
                    //match_analysis_result mar = new match_analysis_result();

                    mar = matches.Match_analysis_result
                        .Where(e => e.Live_table_lib_id == liveid)
                        .First();//查找需要更新的数据

                    mar.Live_table_lib_id = r.live_id;
                    mar.Pre_algorithm = "top20";
                    mar.Pre_match_count = r.Top20Count;
                    mar.Home_goals = r.HomeGoals;
                    mar.Away_goals = r.AwayGoals;

                    mar.Home_w = r.hWin;
                    mar.Home_d = r.hDraw;
                    mar.Home_l = r.hLose;

                    mar.Fit_win_loss = r.CureFitWinLoss();
                    mar.Fit_goals = r.CureFitGoals();
                    mar.Fit_odd_even = r.CureFitOddEven();//直接赋值,修改完成

                    //2011.9.22   修改成利用积分进行运算
                    mar.Recent_scores = r.RecentScores; //增加最后一轮的分数
                    mar.Recent_2scores = r.Recent2Scores;
                    mar.Recent_3scores = r.Recent3Scores;
                    mar.Recent_4scores = r.Recent4Scores;
                    mar.Recent_5scores = r.Recent5Scores;
                    mar.Recent_6scores = r.Recent6Scores;


                    //2011.6.22
                    mar.Cross_goals = r.CrossGoals;


                    //2011.6.16
                    //【交战+概率1+拟合+进球+概率30】
                    mar.Myfit =
                        //交战
                        ForecastCross(r.CrossGoals) + ":" +
                        //概率1
                        ForecastD(mar.Home_w, mar.Home_d, mar.Home_l) + ":" +
                        //拟合+进球+概率30
                        ForecastWL(mar.Fit_win_loss, mar.Home_goals, mar.Away_goals, mar.Home_w, mar.Home_l);


                    //更新北京单场
                    sg = lss[r.home_team_big.ToString() + r.away_team_big.ToString()].FirstOrDefault();
                    if (sg != null)
                        mar.Pre_algorithm = sg.Html_position;

                    sgerror = lss[r.away_team_big.ToString() + r.home_team_big.ToString()].FirstOrDefault();
                    if (sgerror != null)
                        mar.Pre_algorithm = sgerror.Html_position;

                    //r.Close();

                    //缩短数据更新周期
                    if (i % 100 == 0)
                    {
                        matches.SubmitChanges();
                        //GC.Collect(); GC.Collect(); Application.DoEvents();
                    }

                    //2011.10.8.   这里销毁
                    r.Dispose();
                }
                matches.SubmitChanges();
            }
        }
        public void InitMatchNow()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                var matchnowf = from p in matches.Match_analysis_result
                                join t in matches.Live_Table_lib on p.Live_table_lib_id equals t.Live_table_lib_id
                                where p.Result_tb_lib_id == null || p.Result_fit == "no forecast"
                                select new
                                {
                                    p.Analysis_result_id,
                                    t.Match_time,
                                    t.Match_type,
                                    t.Home_team,
                                    t.Away_team,
                                    t.Status,

                                    p.Home_w,
                                    p.Home_d,
                                    p.Home_l,
                                    p.Home_goals,
                                    p.Away_goals,

                                    p.Cross_goals,
                                    p.Fit_win_loss,

                                    p.Recent_scores,
                                    p.Recent_2scores,
                                    p.Recent_3scores,
                                    p.Recent_4scores,
                                    p.Recent_5scores,
                                    p.Recent_6scores,

                                    Dodds = ConvertOdd(t.Home_team, t.Status)
                                };
                MatlabMatch.matchnow = matchnowf.ToDataTable();
            }
        }