コード例 #1
0
        void UpdateCurrentworker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                //var listMain = (from a in grdPostCodes.Rows.Where(c => c.Cells[COLS.Check].Value.ToBool() == true)
                //                select new
                //                {
                //                    Id = a.Cells[COLS.Id].Value,
                //                    PostCode = a.Cells[COLS.PostCode].Value.ToStr()
                //                }).ToList();

                GridViewRowInfo row = grdSelectedPostCodes.CurrentRow;
                if (row.Cells[COLS.Id].Value.ToInt() == 0)
                {
                    //e.Result
                    return;
                }
                bool allowUpdating = false;
                //foreach (var item in listMain)
                //{

                using (TaxiDataContext db = new TaxiDataContext())
                {
                    allowUpdating = false;

                    var listofCoordinates = db.stp_GetCoordinatesByRoadLevelData(row.Cells[COLS.Id].Value.ToInt(), string.Empty).ToList();

                    //row.Cells[COLS.PostCode].Value
                    char PostCodeShort = row.Cells[COLS.PostCode].Value.ToStr()[0];

                    //if (item.PostCode.Length == 1 && listofCoordinates.Count != db.Gen_Coordinates.Count(c => c.PostCode[0] == item.PostCode[0] && SqlMethods.Like(c.PostCode, "[A-Z][0-9]%")))
                    if (PostCodeShort.ToStr().Length == 1 && listofCoordinates.Count != db.Gen_Coordinates.Count(c => c.PostCode[0] == PostCodeShort && SqlMethods.Like(c.PostCode, "[A-Z][0-9]%")))
                    {
                        allowUpdating = true;
                    }
                    else if (row.Cells[COLS.PostCode].Value.ToStr().Length > 1 && listofCoordinates.Count != db.Gen_Coordinates.Count(c => c.PostCode.StartsWith(row.Cells[COLS.PostCode].Value.ToStr())))
                    {
                        allowUpdating = true;
                    }

                    if (allowUpdating)
                    {
                        CurrentRow cr = new CurrentRow();
                        cr.Total = listofCoordinates.Count;
                        //cr.UpdateValue = item.PostCode;
                        cr.UpdateValue = row.Cells[COLS.PostCode].Value.ToStr();

                        int cnter = 0;
                        // int updatecounter = 0;
                        foreach (var item2 in listofCoordinates)
                        {
                            try
                            {
                                if (IsFormClosed)
                                {
                                    break;
                                }

                                db.Gen_Coordinates.InsertOnSubmit(new Gen_Coordinate {
                                    PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude
                                });
                                db.SubmitChanges();
                            }
                            catch
                            {
                            }

                            cnter++;
                            cr.index = cnter;
                            UpdateCurrentworker.ReportProgress(cnter, cr);
                        }
                        //List<Gen_Coordinate> cc = new List<Gen_Coordinate>();
                        //foreach (var item2 in listofCoordinates)
                        //{
                        //    try
                        //    {

                        //        if (IsFormClosed)
                        //            break;

                        //        if (cc.Count < 500)
                        //        {
                        //            cc.Add(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude });
                        //        }
                        //        else
                        //        {
                        //            db.Gen_Coordinates.InsertAllOnSubmit(cc);
                        //            //db.Gen_Coordinates.InsertOnSubmit(new Gen_Coordinate { PostCode = item2.PostCode, Latitude = item2.Latitude, Longitude = item2.Longitude });
                        //            db.SubmitChanges();

                        //            if (listofCoordinates.Count + 500 > listofCoordinates.Count)
                        //            {
                        //                cnter = listofCoordinates.Count;
                        //            }
                        //            else
                        //            {
                        //                cnter += 500;
                        //            }
                        //            cc.Clear();


                        //            cr.index = cnter;
                        //            UpdateCurrentworker.ReportProgress(cnter, cr);
                        //        }

                        //    }
                        //    catch
                        //    {


                        //    }



                        //  //  updatecounter++;
                        //    //cnter++;
                        //    //cr.index = cnter;
                        //    //UpdateCurrentworker.ReportProgress(cnter, cr);

                        //}
                    }
                }
                //if (IsFormClosed)
                //    break;
                //}
                e.Result = "Completed";
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
            }
        }