protected bool writeValues(statdataEntities ctx, IEnumerable<ValueDesc> oVals)
 {
     bool bRet = false;
     foreach (var val in oVals)
     {
         if (val != null)
         {
             int nId = val.Id;
             int index = val.Index;
             int nVarId = val.VariableId;
             String sval = StatHelpers.ConvertValue(val.DataStringValue);
             DbValue pVal = null;
             if (nId != 0)
             {
                 var q = from x in ctx.DbValues where x.Id == nId select x;
                 if (q.Count() > 0)
                 {
                     pVal = q.First();
                     pVal.Value = sval;
                     bRet = true;
                 }
             }
             if (pVal == null)
             {
                 var q = from x in ctx.DbValues where (x.VariableId == nVarId) && (x.Index == index) select x;
                 if (q.Count() > 0)
                 {
                     pVal = q.First();
                     pVal.Value = sval;
                     bRet = true;
                 }
             }
             if (pVal == null)
             {
                 pVal = new DbValue();
                 pVal.Id = nextId(ctx, TAB_VALEUR);
                 pVal.VariableId = nVarId;
                 pVal.Index = index;
                 pVal.Value = sval;
                 ctx.DbValues.Add(pVal);
                 bRet = true;
             }
         }// val
     }// val
     return bRet;
 }
 public Tuple<bool, Exception> WriteValues(IEnumerable<ValueDesc> vals)
 {
     bool bRet = false;
     Exception err = null;
     if (vals == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentNullException());
     }
     try
     {
         int nMax = 0;
         foreach (var v in vals)
         {
             if ((v != null) && (v.Id == 0) && (v.Index >= 0))
             {
                 ++nMax;
             }
         }// v
         int[] ids = null;
         using (var ctx = getContext())
         {
             if (nMax > 0)
             {
                 ids = nextIds(ctx, TAB_VALEUR, nMax);
             }
             int icur = 0;
             HashSet<int> oSet = new HashSet<int>();
             foreach (var v in vals)
             {
                 if ((v != null) && (v.Index >= 0))
                 {
                     DbValue pVal = null;
                     if (v.Id != 0)
                     {
                         var q = from x in ctx.DbValues where x.Id == v.Id select x;
                         if (q.Count() > 0)
                         {
                             pVal = q.First();
                             pVal.Index = v.Index;
                             pVal.Value = StatHelpers.ConvertValue(v.DataStringValue);
                         }
                     }
                     else
                     {
                         int nVarId = v.VariableId;
                         if (nVarId == 0)
                         {
                             continue;
                         }
                         if (!oSet.Contains(nVarId))
                         {
                             var qx = from x in ctx.DbVariables where x.Id == nVarId select x.Id;
                             if (qx.Count() < 1)
                             {
                                 continue;
                             }
                             oSet.Add(nVarId);
                         }
                         var q = from x in ctx.DbValues
                                 where (x.VariableId == nVarId) &&
                                     (x.Index == v.Index)
                                 select x;
                         if (q.Count() > 0)
                         {
                             pVal = q.First();
                             pVal.Value = StatHelpers.ConvertValue(v.DataStringValue);
                         }
                         else
                         {
                             pVal = new DbValue();
                             pVal.Id = ids[icur++];
                             pVal.VariableId = nVarId;
                             pVal.Index = v.Index;
                             pVal.Value = StatHelpers.ConvertValue(v.DataStringValue);
                             ctx.DbValues.Add(pVal);
                             ctx.SaveChanges();
                         }
                     }
                 }// v
             }// v
             ctx.SaveChanges();
             bRet = true;
         }// ctx
     }// try
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<bool, Exception>(bRet, err);
 }
 protected void convertValue(DbValue p, ValueDesc pp)
 {
     pp.Id = p.Id;
     pp.VariableId = p.VariableId;
     pp.Index = p.Index;
     pp.DataStringValue = p.Value;
     pp.VariableName = p.Variable.Name;
     pp.VariableType = p.Variable.VarType;
     pp.IsModified = false;
 }
 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);
 }