protected void convertDataSet(DbDataSet p, StatDataSet pp)
 {
     pp.Id = p.Id;
     pp.LastIndex = p.LastIndex;
     pp.Name = p.Name;
     pp.Description = p.Description;
     pp.IsModified = false;
 }
 protected DbDataSet maintainsDataSet(statdataEntities ctx, StatDataSet oSet)
 {
     if (oSet == null)
     {
         return null;
     }
     String sname = oSet.Name.Trim();
     if (String.IsNullOrEmpty(sname))
     {
         return null;
     }
     DbDataSet pRet = findDataSet(ctx, oSet);
     if (pRet != null)
     {
         pRet.Name = oSet.Name;
         pRet.Description = oSet.Description;
     }
     else
     {
         pRet = new DbDataSet();
         pRet.Id = nextId(ctx, TAB_DATASET);
         pRet.LastIndex = 0;
         pRet.Name = sname;
         pRet.Description = oSet.Description;
         ctx.DbDataSets.Add(pRet);
         ctx.SaveChanges();
     }
     return pRet;
 }
 public Tuple<bool, Exception> ReplaceDataSet(StatDataSet oSet, CancellationToken cancelletionToken, IProgress<int> progress)
 {
     if (cancelletionToken.IsCancellationRequested)
     {
         return new Tuple<bool, Exception>(false, null);
     }
     if (oSet == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     String sRealDataSetName = oSet.Name;
     if (String.IsNullOrEmpty(sRealDataSetName))
     {
         return new Tuple<bool, Exception>(false, new ArgumentException());
     }
     String tempName = String.Format("{0}_tmpwork", sRealDataSetName);
     if (progress != null)
     {
         progress.Report(1);
     }
     int nTotalValues = 0;
     int nTotalVars = 0;
     foreach (var v in oSet.Variables)
     {
         ++nTotalVars;
         nTotalValues += v.Values.Count();
     }// v
     if ((nTotalVars < 1) || (nTotalValues < 1))
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     if (cancelletionToken.IsCancellationRequested)
     {
         return new Tuple<bool, Exception>(false, null);
     }
     double dTotal = (double)(2 * nTotalVars + 5);
     double dCur = 1;
     bool bRet = false;
     Exception err = null;
     bool bCont = true;
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = null;
             var qq = from x in ctx.DbDataSets where x.Name.Trim().ToLower() == tempName.Trim().ToLower() select x;
             if (qq.Count() > 0)
             {
                 pSet = qq.First();
             }
             if (pSet != null)
             {
                 ctx.DbDataSets.Remove(pSet);
                 ctx.SaveChanges();
                 pSet = null;
             }// pSet
             if (progress != null)
             {
                 dCur = dCur + 1.0;
                 int nx = (int)((dCur / dTotal) * 100.0 + 0.5);
                 progress.Report(nx);
             }
             int nDataSetId = nextId(ctx, TAB_DATASET);
             int[] varIndexes = nextIds(ctx, TAB_VARIABLE, nTotalVars);
             int[] valIndexes = nextIds(ctx, TAB_VALEUR, nTotalValues);
             pSet = new DbDataSet();
             pSet.Id = nDataSetId;
             pSet.Name = tempName;
             pSet.Description = oSet.Description;
             ctx.DbDataSets.Add(pSet);
             nDataSetId = pSet.Id;
             int iCurrentVar = 0;
             int iCurrentVal = 0;
             HashSet<String> oSetNames = new HashSet<string>();
             foreach (var v in oSet.Variables)
             {
                 if (!bCont)
                 {
                     break;
                 }
                 if (v == null)
                 {
                     continue;
                 }
                 if (cancelletionToken.IsCancellationRequested)
                 {
                     bCont = false;
                     break;
                 }
                 String name = v.Name;
                 String stype = v.DataType;
                 if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(stype))
                 {
                     continue;
                 }
                 if (oSetNames.Contains(name))
                 {
                     continue;
                 }
                 DbVariable vv = new DbVariable();
                 int nVarId = varIndexes[iCurrentVar++];
                 vv.Id = nVarId;
                 vv.DataSetId = pSet.Id;
                 vv.Name = name;
                 vv.VarType = stype;
                 vv.Description = v.Description;
                 vv.IsCateg = v.IsCategVar;
                 vv.IsId = v.IsIdVar;
                 vv.IsImageVar = v.IsImageVar;
                 vv.IsInfoVar = v.IsInfoVar;
                 vv.IsName = v.IsNameVar;
                 ctx.DbVariables.Add(vv);
                 HashSet<int> oSetIndexes = new HashSet<int>();
                 foreach (var vx in v.Values)
                 {
                     if (!bCont)
                     {
                         break;
                     }
                     if (vx == null)
                     {
                         continue;
                     }
                     int ind = vx.Index;
                     if ((ind < 0) || oSetIndexes.Contains(ind))
                     {
                         continue;
                     }
                     oSetIndexes.Add(ind);
                     DbValue vz = new DbValue();
                     vz.Id = valIndexes[iCurrentVal++];
                     vz.VariableId = nVarId;
                     vz.Index = ind;
                     vz.Value = StatHelpers.ConvertValue(vx.DataStringValue);
                     ctx.DbValues.Add(vz);
                 }// vx
                 if (progress != null)
                 {
                     dCur = dCur + 1.0;
                     int nx = (int)((dCur / dTotal) * 100.0 + 0.5);
                     progress.Report(nx);
                 }
             }// v
             if (bCont)
             {
                 ctx.SaveChanges();
                 var qqq = from x in ctx.DbDataSets where x.Name.Trim().ToLower() == tempName.Trim().ToLower() select x;
                 if (qqq.Count() > 0)
                 {
                     pSet = qqq.First();
                     pSet.Name = sRealDataSetName;
                     ctx.SaveChanges();
                     bRet = true;
                 }
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<bool, Exception>(bRet, err);
 }