public Tuple<IEnumerable<VariableDesc>, Exception> GetDataSetVariablesAndData(StatDataSet oSet)
 {
     List<VariableDesc> oRet = null;
     Exception err = null;
     if (oSet == null)
     {
         return new Tuple<IEnumerable<VariableDesc>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = findDataSet(ctx, oSet);
             if (pSet != null)
             {
                 var q = from x in pSet.Variables orderby x.Name ascending select x;
                 oRet = new List<VariableDesc>();
                 foreach (var p in q)
                 {
                     VariableDesc pp = new VariableDesc();
                     convertVariable(p, pp);
                     List<ValueDesc> oList = new List<ValueDesc>();
                     var col = p.Values;
                     foreach (var v in col)
                     {
                         if (v.Index >= 0)
                         {
                             ValueDesc vv = new ValueDesc();
                             convertValue(v, vv);
                             oList.Add(vv);
                         }// v
                     }// v
                     pp.Values = new ValueDescs(oList);
                     var xx = getVariableInfo(ctx, pp);
                     if (xx.Item2 != null)
                     {
                         return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err);
                     }
                     pp.Info = xx.Item1;
                     oRet.Add(pp);
                 }// p
             }// pSet
         }// ctx
         if ((oRet != null) && (oRet.Count > 1))
         {
             oRet.Sort();
         }
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IEnumerable<VariableDesc>, Exception>(oRet, err);
 }
        public Tuple<IEnumerable<ValueDesc>, Exception> GetVariableValues(VariableDesc oVar, int skip, int taken)
        {
            List<ValueDesc> oRet = new List<ValueDesc>();
            Exception err = null;
            if (oVar == null)
            {
                return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentNullException());
            }
            try
            {
                using (var ctx = getContext())
                {
                    DbVariable pVar = findVariable(ctx, oVar);
                    if (pVar == null)
                    {
                        return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentException());
                    }
                    int nMax = pVar.Values.Count();
                    if (skip < 0)
                    {
                        skip = 0;
                    }
                    if (taken < 0)
                    {
                        taken = 0;
                    }
                    if ((skip + taken) > nMax)
                    {
                        taken = nMax - skip;
                        if (taken < 0)
                        {
                            taken = 0;
                        }
                    }
                    if (taken == 0)
                    {
                        if (skip > 0)
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Skip(skip);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = from x in pVar.Values orderby x.Index ascending select x;
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }

                    }
                    else
                    {
                        if (skip > 0)
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Skip(skip).Take(taken);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                        else
                        {
                            var q = (from x in pVar.Values orderby x.Index ascending select x).Take(taken);
                            foreach (var p in q)
                            {
                                ValueDesc pp = new ValueDesc();
                                convertValue(p, pp);
                                oRet.Add(pp);
                            }// p
                        }
                    }
                }// ctx
            }
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IEnumerable<ValueDesc>, Exception>(oRet, err);
        }
 public Tuple<IndivDescs, Exception> GetDataSetIndivs(StatDataSet oSet)
 {
     if (oSet == null)
     {
         return new Tuple<IndivDescs, Exception>(null, new ArgumentNullException());
     }
     IndivDescs oRet = null;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             DbDataSet pSet = findDataSet(ctx, oSet);
             if (pSet == null)
             {
                 return new Tuple<IndivDescs, Exception>(oRet, err);
             }
             DbVariable pIdVar = null;
             DbVariable pNameVar = null;
             DbVariable pImageVar = null;
             var q = from x in pSet.Variables select x;
             foreach (var v in q)
             {
                 if (v.IsId)
                 {
                     pIdVar = v;
                 }
                 if (v.IsName)
                 {
                     pNameVar = v;
                 }
                 if (v.IsImageVar)
                 {
                     pImageVar = v;
                 }
             }// v
             bool bImageInt = false;
             if (pImageVar != null)
             {
                 String s = pImageVar.VarType.Trim().ToLower();
                 if ((s != "string") && (s != "bool"))
                 {
                     bImageInt = true;
                 }
             }
             List<IndivDesc> oList = new List<IndivDesc>();
             var indexes = (from x in ctx.DbValues where x.Variable.DataSetId == pSet.Id select x.Index).Distinct();
             foreach (var ind in indexes)
             {
                 IndivDesc pp = new IndivDesc();
                 pp.IndivIndex = ind;
                 if (pIdVar != null)
                 {
                     var qx = from x in ctx.DbValues where (x.VariableId == pIdVar.Id) && (x.Index == ind) select x;
                     if (qx.Count() > 0)
                     {
                         pp.IdString = qx.First().Value;
                     }
                 }// Id
                 if (pNameVar != null)
                 {
                     var qx = from x in ctx.DbValues where (x.VariableId == pNameVar.Id) && (x.Index == ind) select x;
                     if (qx.Count() > 0)
                     {
                         pp.Name = qx.First().Value;
                     }
                 }// name
                 if (pImageVar != null)
                 {
                     var qx = from x in ctx.DbValues where (x.VariableId == pImageVar.Id) && (x.Index == ind) select x;
                     if (qx.Count() > 0)
                     {
                         var px = qx.First();
                         String sx = StatHelpers.ConvertValue(px.Value);
                         if (!String.IsNullOrEmpty(sx))
                         {
                             if (bImageInt)
                             {
                                 double dval = 0.0;
                                 if (double.TryParse(sx, out dval))
                                 {
                                     int ival = (int)dval;
                                     if (ival != 0)
                                     {
                                         var qz = from x in ctx.DbPhotoes where x.Id == ival select x;
                                         if (qz.Count() > 0)
                                         {
                                             var pz = qz.First();
                                             pp.PhotoId = pz.Id;
                                             pp.PhotoData = pz.DataBytes;
                                         }
                                     }// ival
                                 }
                             }
                             else
                             {
                                 var qz = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == sx.ToLower() select x;
                                 if (qz.Count() > 0)
                                 {
                                     var pz = qz.First();
                                     pp.PhotoId = pz.Id;
                                     pp.PhotoData = pz.DataBytes;
                                 }
                             }// ival
                         }// sx
                     }
                 }// photo
                 ValueDescs vv = new ValueDescs();
                 var qq = from x in ctx.DbValues where (x.Variable.DataSetId == pSet.Id) && (x.Index == ind) select x;
                 foreach (var v in qq)
                 {
                     ValueDesc fx = new ValueDesc();
                     convertValue(v, fx);
                     vv.Add(fx);
                 }// v
                 pp.Values = vv;
                 oList.Add(pp);
             }// ind
             if (oList.Count > 1)
             {
                 oList.Sort();
             }
             oRet = new IndivDescs(oList);
         }// ctx
     }// try
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IndivDescs, Exception>(oRet, err);
 }
 public Tuple<IEnumerable<ValueDesc>, Exception> GetDataSetIndivValues(StatDataSet oSet, int iIndex)
 {
     if (oSet == null)
     {
         return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentNullException());
     }
     if (iIndex < 0)
     {
         return new Tuple<IEnumerable<ValueDesc>, Exception>(null, new ArgumentOutOfRangeException());
     }
     List<ValueDesc> oRet = null;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             oRet = new List<ValueDesc>();
             var pSet = findDataSet(ctx, oSet);
             if (pSet != null)
             {
                 var q = from x in ctx.DbValues
                         where (x.Variable.DataSetId == pSet.Id) &&
                         (x.Index == iIndex)
                         select x;
                 foreach (var p in q)
                 {
                     ValueDesc pp = new ValueDesc();
                     convertValue(p, pp);
                     oRet.Add(pp);
                 }// p
             }
         }// ctx
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<IEnumerable<ValueDesc>, Exception>(oRet, err);
 }
 protected Tuple<Dictionary<VariableDesc, Dictionary<int, ValueDesc>>, Exception> readValues(statdataEntities ctx, IEnumerable<VariableDesc> oVars, IEnumerable<int> oIndexes)
 {
     Dictionary<VariableDesc, Dictionary<int, ValueDesc>> oDictRet = null;
     Exception err = null;
     if ((oVars == null) || (oIndexes == null))
     {
         return new Tuple<Dictionary<VariableDesc, Dictionary<int, ValueDesc>>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         oDictRet = new Dictionary<VariableDesc, Dictionary<int, ValueDesc>>();
         foreach (var oVar in oVars)
         {
             DbVariable pVar = findVariable(ctx, oVar);
             if (pVar == null)
             {
                 return new Tuple<Dictionary<VariableDesc, Dictionary<int, ValueDesc>>, Exception>(null, new ArgumentException());
             }
             var curDict = new Dictionary<int, ValueDesc>();
             foreach (var ind in oIndexes)
             {
                 if (ind >= 0)
                 {
                     var qq = from x in pVar.Values where x.Index == ind select x;
                     if (qq.Count() > 0)
                     {
                         var v = qq.First();
                         var vv = new ValueDesc();
                         convertValue(v, vv);
                         curDict[ind] = vv;
                     }
                 }// ind
             }// ind
             oDictRet[oVar] = curDict;
         }// oVar
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<Dictionary<VariableDesc, Dictionary<int, ValueDesc>>, Exception>(oDictRet, err);
 }
        public Tuple<IndivDescs, VariableDescs, Exception> FetchAllDataSetData(StatDataSet oSet)
        {
            IndivDescs indivs = null;
            VariableDescs vars = null;
            Exception err = null;
            try
            {
                using (var ctx = getContext())
                {
                    DbDataSet pSet = findDataSet(ctx, oSet);
                    if (pSet == null)
                    {
                        return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
                    }
                    DbVariable pIdVar = null;
                    DbVariable pNameVar = null;
                    DbVariable pImageVar = null;
                    var q = from x in pSet.Variables orderby x.Name select x;
                    foreach (var v in q)
                    {
                        if (v.IsId)
                        {
                            pIdVar = v;
                        }
                        if (v.IsName)
                        {
                            pNameVar = v;
                        }
                        if (v.IsImageVar)
                        {
                            pImageVar = v;
                        }
                    }// v
                    bool bImageInt = false;
                    if (pImageVar != null)
                    {
                        String s = pImageVar.VarType.Trim().ToLower();
                        if ((s != "string") && (s != "bool"))
                        {
                            bImageInt = true;
                        }
                    }
                    List<VariableDesc> oRet = new List<VariableDesc>();
                    HashSet<int> oSetIndex = new HashSet<int>();
                    List<IndivDesc> listIndivs = new List<IndivDesc>();
                    Dictionary<int, ValueDescs> vals = new Dictionary<int, ValueDescs>();
                    foreach (var p in q)
                    {
                        VariableDesc pp = new VariableDesc();
                        convertVariable(p, pp);
                        var col = p.Values;
                        List<ValueDesc> pList = new List<ValueDesc>();
                        foreach (var v in col)
                        {
                            int index = v.Index;
                            if (index >= 0)
                            {
                                ValueDesc vv = new ValueDesc();
                                convertValue(v, vv);
                                pList.Add(vv);
                                if (!vals.ContainsKey(index))
                                {
                                    vals[index] = new ValueDescs();
                                }
                                (vals[index]).Add(vv);
                                if (!oSetIndex.Contains(index))
                                {
                                    oSetIndex.Add(index);
                                    var ind = new IndivDesc();
                                    listIndivs.Add(ind);
                                    ind.IndivIndex = index;
                                    if (pIdVar != null)
                                    {
                                        var qx = from x in pIdVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            ind.IdString = qx.First().Value;
                                        }
                                    }// Id
                                    if (pNameVar != null)
                                    {
                                        var qx = from x in pNameVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            ind.Name = qx.First().Value;
                                        }
                                    }// name
                                    if (pImageVar != null)
                                    {
                                        var qx = from x in pImageVar.Values where x.Index == index select x;
                                        if (qx.Count() > 0)
                                        {
                                            var px = qx.First();
                                            String sx = StatHelpers.ConvertValue(px.Value);
                                            if (!String.IsNullOrEmpty(sx))
                                            {
                                                if (bImageInt)
                                                {
                                                    double dval = 0.0;
                                                    if (double.TryParse(sx, out dval))
                                                    {
                                                        int ival = (int)dval;
                                                        if (ival != 0)
                                                        {
                                                            var qz = from x in ctx.DbPhotoes where x.Id == ival select x;
                                                            if (qz.Count() > 0)
                                                            {
                                                                var pz = qz.First();
                                                                ind.PhotoId = pz.Id;
                                                                ind.PhotoData = pz.DataBytes;
                                                            }
                                                        }// ival
                                                    }
                                                }
                                                else
                                                {
                                                    var qz = from x in ctx.DbPhotoes where x.Name.Trim().ToLower() == sx.ToLower() select x;
                                                    if (qz.Count() > 0)
                                                    {
                                                        var pz = qz.First();
                                                        ind.PhotoId = pz.Id;
                                                        ind.PhotoData = pz.DataBytes;
                                                    }
                                                }// ival
                                            }// sx
                                        }
                                    }// photo

                                }// add value
                            }// v
                        }// v
                        var xx = getVariableInfo(ctx, pp);
                        if (xx.Item2 != null)
                        {
                            return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
                        }
                        pp.Info = xx.Item1;
                        pp.Values = new ValueDescs(pList);
                        oRet.Add(pp);
                    }// p
                    foreach (var ind in listIndivs)
                    {
                        int index = ind.IndivIndex;
                        if (vals.ContainsKey(index))
                        {
                            ind.Values = vals[index];
                        }
                    }// ind
                    if (listIndivs.Count > 1)
                    {
                        listIndivs.Sort();
                    }
                    if (oRet.Count > 1)
                    {
                        oRet.Sort();
                    }
                    indivs = new IndivDescs(listIndivs);
                    vars = new VariableDescs(oRet);
                }// ctx
            }// try
            catch (Exception ex)
            {
                err = ex;
            }
            return new Tuple<IndivDescs, VariableDescs, Exception>(indivs, vars, err);
        }
 protected Tuple<Dictionary<string, List<ValueDesc>>, Exception> readCategValues(statdataEntities ctx,
     VariableDesc oCategVar, VariableDesc oDataVar)
 {
     Exception err = null;
     Dictionary<String, List<ValueDesc>> xRet = null;
     if ((oCategVar == null) || (oDataVar == null))
     {
         return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(null, new ArgumentNullException());
     }
     try
     {
         DbVariable pCategVar = findVariable(ctx, oCategVar);
         DbVariable pDataVar = findVariable(ctx, oDataVar);
         if ((pCategVar == null) || (pDataVar == null))
         {
             return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(null, new ArgumentException());
         }
         xRet = new Dictionary<string, List<ValueDesc>>();
         var qcateg = from x in ctx.DbValues where x.VariableId == pCategVar.Id select x;
         foreach (var vcateg in qcateg)
         {
             int index = vcateg.Index;
             String scateg = StatHelpers.ConvertValue(vcateg.Value);
             if ((index >= 0) && (!String.IsNullOrEmpty(scateg)))
             {
                 var qdata = from x in ctx.DbValues where (x.VariableId == pDataVar.Id) && (x.Index == index) select x;
                 foreach (var vdata in qdata)
                 {
                     String sdata = StatHelpers.ConvertValue(vdata.Value);
                     if (!String.IsNullOrEmpty(sdata))
                     {
                         ValueDesc vv = new ValueDesc();
                         convertValue(vdata, vv);
                         if (!xRet.ContainsKey(scateg))
                         {
                             List<ValueDesc> ll = new List<ValueDesc>();
                             ll.Add(vv);
                             xRet[scateg] = ll;
                         }
                         else
                         {
                             (xRet[scateg]).Add(vv);
                         }
                     }// sdata
                 }// vdata
             }// vcateg
         }// vcateg
     }
     catch (Exception ex)
     {
         err = ex;
     }
     return new Tuple<Dictionary<string, List<ValueDesc>>, Exception>(xRet, err);
 }
 protected Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception> readCategValues(statdataEntities ctx,
     VariableDesc oCategVar, IEnumerable<VariableDesc> oDataVars)
 {
     if ((oCategVar == null) || (oDataVars == null))
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentNullException());
     }
     List<VariableDesc> oList = new List<VariableDesc>();
     oList.Add(oCategVar);
     oList.AddRange(oDataVars);
     var xx = getIndexes(ctx, oList);
     if (xx == null)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentException());
     }
     var oIndexes = xx.Item1;
     if ((oIndexes == null) || (xx.Item2 != null))
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, xx.Item2);
     }
     Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>> oRet = new Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>();
     DbVariable pCategVar = findVariable(ctx, oCategVar);
     if (pCategVar == null)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, new ArgumentException());
     }
     var qcateg = pCategVar.Values;
     foreach (var ind in oIndexes)
     {
         var q = from x in qcateg where x.Index == ind select x;
         if (q.Count() > 0)
         {
             DbValue vcateg = q.First();
             String scateg = StatHelpers.ConvertValue(vcateg.Value);
             if (!String.IsNullOrEmpty(scateg))
             {
                 if (!oRet.ContainsKey(scateg))
                 {
                     oRet[scateg] = new Dictionary<VariableDesc, List<ValueDesc>>();
                 }
                 var dict = oRet[scateg];
                 foreach (var vdata in oDataVars)
                 {
                     if (!dict.ContainsKey(vdata))
                     {
                         dict[vdata] = new List<ValueDesc>();
                     }
                     List<ValueDesc> xlist = dict[vdata];
                     var qy = from x in ctx.DbValues where (x.VariableId == vdata.Id) && (x.Index == ind) select x;
                     if (qy.Count() > 0)
                     {
                         var v = qy.First();
                         String s = StatHelpers.ConvertValue(v.Value);
                         if (!String.IsNullOrEmpty(s))
                         {
                             ValueDesc vz = new ValueDesc();
                             convertValue(v, vz);
                             xlist.Add(vz);
                         }
                     }// qy
                 }// vdata
             }// scateg
         }// qCount
     }// ind
     if (oRet.Count < 1)
     {
         return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(null, null);
     }
     return new Tuple<Dictionary<string, Dictionary<VariableDesc, List<ValueDesc>>>, Exception>(oRet, null);
 }
 public Tuple<bool, Exception> MaintainsVariableAndValues(IEnumerable<VariableDesc> oVars)
 {
     if (oVars == null)
     {
         return new Tuple<bool, Exception>(false, new ArgumentException());
     }
     bool bRet = false;
     Exception err = null;
     try
     {
         using (var ctx = getContext())
         {
             bool xRet = false;
             foreach (var oVar in oVars)
             {
                 var v = maintainsVariable(ctx, oVar);
                 if (v == null)
                 {
                     return new Tuple<bool, Exception>(false, new ArgumentException());
                 }
                 xRet = true;
                 List<ValueDesc> oVals = new List<ValueDesc>();
                 foreach (var vx in oVar.Values)
                 {
                     if (vx != null)
                     {
                         ValueDesc vy = new ValueDesc();
                         vy.Id = vx.Id;
                         vy.VariableId = v.Id;
                         vy.Index = vx.Index;
                         vy.DataStringValue = vx.DataStringValue;
                         oVals.Add(vy);
                     }
                 }// vx
                 if (oVals.Count > 0)
                 {
                     xRet = xRet && writeValues(ctx, oVals);
                 }
             }// oVar
             if (xRet)
             {
                 ctx.SaveChanges();
                 bRet = true;
             }
         }// ctx
     }
     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;
 }
 }// RefreshValues
 private void buttonAdd_Click(object sender, RoutedEventArgs e)
 {
     var model = getModel();
     if (model == null)
     {
         return;
     }
     m_busy = true;
     var oVar = model.CurrentVariable;
     if (oVar != null)
     {
         ValueDescs vv = model.Values;
         int nMax = 0;
         if (vv.Count > 0)
         {
             nMax = (from x in vv select x.Index).Max();
             ++nMax;
         }
         int nVarId = oVar.Id;
         ValueDesc v = new ValueDesc();
         v.VariableId = nVarId;
         v.Index = nMax;
         vv.Add(v);
         model.NotifyChange("Values");
     }// oVar
     m_busy = false;
 }
 private async void buttonAdd_Click(object sender, RoutedEventArgs e)
 {
     var model = getModel();
     if (model == null)
     {
         return;
     }
     var oSet = model.CurrentStatDataSet;
     if (oSet == null)
     {
         return;
     }
     var pMan = model.DataService;
     if ((!oSet.IsValid) || (pMan == null))
     {
         return;
     }
     VariableDesc vv = new VariableDesc();
     vv.DataSetId = oSet.Id;
     vv.Name = "ArrangeIndex";
     vv.DataType = "short";
     vv.IsCategVar = true;
     vv.Description = "Matrix Arrange";
     this.buttonAdd.IsEnabled = false;
     this.buttonCompute.IsEnabled = false;
     model.IsBusy = true;
     var yy = await Task.Run<Tuple<bool, Exception>>(() =>
     {
         return pMan.RemoveVariable(vv);
     });
     if ((yy != null) && (yy.Item2 != null))
     {
         this.buttonAdd.IsEnabled = true;
         this.buttonCompute.IsEnabled = true;
         model.ShowError(yy.Item2);
         model.IsBusy = false;
         return;
     }
     var xx = await Task.Run<Tuple<VariableDesc, Exception>>(() =>
     {
         return pMan.MaintainsVariable(vv);
     });
     if ((xx != null) && (xx.Item1 != null) && (xx.Item2 == null))
     {
         VariableDesc oVar = xx.Item1;
         int nVarId = oVar.Id;
         List<ValueDesc> vals = new List<ValueDesc>();
         foreach (var ind in model.Individus)
         {
             int ival = ind.Position;
             int index = ind.IndivIndex;
             if ((ival >= 0) && (index >= 0))
             {
                 ValueDesc v = new ValueDesc();
                 v.VariableId = nVarId;
                 v.Index = index;
                 v.DataStringValue = Convert.ToString(ival);
                 v.IsModified = true;
                 vals.Add(v);
             }
         }// ind
         if (vals.Count > 0)
         {
             var zz = await Task.Run<Tuple<bool, Exception>>(() =>
             {
                 return pMan.WriteValues(vals);
             });
             if ((zz != null) && (zz.Item2 != null))
             {
                 model.ShowError(zz.Item2);
             }
         }// vals
     }
     else if ((xx != null) && (xx.Item2 != null))
     {
         model.ShowError(xx.Item2);
     }
     model.IsBusy = false;
     this.buttonAdd.IsEnabled = true;
     this.buttonCompute.IsEnabled = true;
     myUpdateUI();
 }
 }// RemoveVariables
 public async void AddToDataSet()
 {
     var pAna = this.Anacompo;
     if (pAna == null)
     {
         return;
     }
     if (!pAna.IsValid)
     {
         return;
     }
     var oSet = this.CurrentStatDataSet;
     if (oSet == null)
     {
         return;
     }
     if (!oSet.IsValid)
     {
         return;
     }
     var inds = pAna.RecodedInds;
     if (inds == null)
     {
         return;
     }
     var oIndexes = pAna.Indexes;
     if (oIndexes == null)
     {
         return;
     }
     int nMax = this.FactorCount;
     int nv = this.FactorCount;
     if (nv < 1)
     {
         nv = 3;
     }
     if (nv > nMax)
     {
         nv = nMax;
     }
     int nr = inds.GetLength(0);
     var pMan = this.DataService;
     if (pMan == null)
     {
         return;
     }
     this.IsBusy = true;
     try
     {
         List<VariableDesc> oVars = new List<VariableDesc>();
         String prefix = this.FactorPrefix;
         for (int i = 0; i < nv; ++i)
         {
             VariableDesc oVar = new VariableDesc();
             oVar.DataSetId = oSet.Id;
             oVar.Name = String.Format("{0}{1}", prefix, i + 1);
             oVar.IsCategVar = false;
             oVar.DataType = "float";
             oVar.Description = String.Format("Composante principale {0}", i + 1);
             oVar.IsModified = true;
             List<ValueDesc> vals = new List<ValueDesc>();
             for (int j = 0; j < nr; ++j)
             {
                 ValueDesc v = new ValueDesc();
                 v.Index = oIndexes[j];
                 v.DataStringValue = Convert.ToString(inds[j, i]);
                 v.IsModified = true;
                 vals.Add(v);
             }// j
             oVar.Values = new ValueDescs(vals);
             oVars.Add(oVar);
         }// i
         var xx = await Task.Run<Tuple<bool, Exception>>(() =>
         {
             return pMan.MaintainsVariableAndValues(oVars);
         });
         if ((xx != null) && (xx.Item2 != null))
         {
             this.ShowError(xx.Item2);
         }
     }
     catch (Exception ex)
     {
         this.ShowError(ex);
     }
     this.IsBusy = false;
 }// addDataSet
 private Task<Tuple<StatDataSet, Exception>> readDataSet(String filename,
     sds.DataSet input, CancellationToken cancellationToken, IProgress<int> progress)
 {
     return Task.Run<Tuple<StatDataSet, Exception>>(() =>
     {
         StatDataSet oSet = null;
         Exception err = null;
         try
         {
             FileInfo info = new FileInfo(filename);
             oSet = new StatDataSet();
             oSet.Name = info.Name;
             var col = input.Variables;
             int nTotal = col.Count;
             int nCur = 0;
             foreach (var v in col)
             {
                 if (progress != null)
                 {
                     progress.Report(nCur++);
                 }
                 if (v.Dimensions.Count != 1)
                 {
                     continue;
                 }
                 VariableDesc vv = new VariableDesc();
                 ValueDescs vals = new ValueDescs();
                 vv.Name = v.Name;
                 Type t = v.TypeOfData;
                 if (t == typeof(String))
                 {
                     vv.DataType = "string";
                     vv.IsCategVar = true;
                     String[] data = input.GetData<String[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             vx.DataStringValue = data[i];
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(double))
                 {
                     vv.DataType = "double";
                     vv.IsCategVar = false;
                     double[] data = input.GetData<double[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(myconvert(data[i]));
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(float))
                 {
                     vv.DataType = "float";
                     vv.IsCategVar = false;
                     float[] data = input.GetData<float[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(myconvert(data[i]));
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(int))
                 {
                     vv.DataType = "int";
                     vv.IsCategVar = false;
                     int[] data = input.GetData<int[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(short))
                 {
                     vv.DataType = "short";
                     vv.IsCategVar = false;
                     short[] data = input.GetData<short[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 else if (t == typeof(bool))
                 {
                     vv.DataType = "bool";
                     vv.IsCategVar = true;
                     bool[] data = input.GetData<bool[]>(v.ID);
                     if ((data != null) && (data.Length > 0))
                     {
                         for (int i = 0; i < data.Length; ++i)
                         {
                             ValueDesc vx = new ValueDesc();
                             vx.Index = i;
                             String s = Convert.ToString(data[i]);
                             vx.DataStringValue = s;
                             vals.Add(vx);
                         }// i
                     }// data
                 }
                 vv.Values = vals;
                 oSet.Variables.Add(vv);
             }// v
         }
         catch (Exception ex)
         {
             err = ex;
         }
         return new Tuple<StatDataSet, Exception>(oSet, err);
     }, cancellationToken);
 }// readDataSet
 }// Dissociate
 public async  void CommitChanges()
 {
     var pMan = this.DataService;
     if (pMan == null)
     {
         return;
     }
     var oSet = m_main.CurrentStatDataSet;
     if (oSet == null)
     {
         return;
     }
     int nSetId = oSet.Id;
     if (nSetId == 0)
     {
         return;
     }
     this.IsBusy = true;
     try
     {
         VariableDesc vIds = m_idvar;
         if (vIds == null)
         {
             String sName = this.IdsVarName;
             if (!String.IsNullOrEmpty(sName))
             {
                 vIds = new VariableDesc();
                 vIds.DataSetId = nSetId;
                 vIds.Name = sName;
                 vIds.IsCategVar = true;
                 vIds.IsIdVar = true;
                 vIds.IsInfoVar = true;
                 vIds.DataType = "string";
             }
         }
         VariableDesc vNames = m_namevar;
         if (vNames == null)
         {
             String sName = this.NamesVarName;
             if (!String.IsNullOrEmpty(sName))
             {
                 vNames = new VariableDesc();
                 vNames.DataSetId = nSetId;
                 vNames.Name = sName;
                 vNames.IsCategVar = true;
                 vNames.IsNameVar = true;
                 vNames.IsInfoVar = true;
                 vNames.DataType = "string";
             }
         }
         VariableDesc vPhotos = m_photovar;
         if (vPhotos == null)
         {
             String sName = this.PhotosVarName;
             if (!String.IsNullOrEmpty(sName))
             {
                 vPhotos = new VariableDesc();
                 vPhotos.DataSetId = nSetId;
                 vPhotos.Name = sName;
                 vPhotos.IsCategVar = false;
                 vPhotos.IsNumVar = true;
                 vPhotos.IsImageVar = true;
                 vNames.IsInfoVar = true;
                 vPhotos.DataType = "int";
             }
         }
         var inds = this.Individus;
         foreach (var ind in inds)
         {
             int index = ind.IndivIndex;
             if (index < 0)
             {
                 continue;
             }
             if (vIds != null)
             {
                 if (vIds.Id != 0)
                 {
                     var q = from x in vIds.Values where x.Index == index select x;
                     if (q.Count() > 0)
                     {
                         var vx = q.First();
                         vx.DataStringValue = ind.IdString;
                         vx.IsModified = true;
                     }
                     else
                     {
                         var vx = new ValueDesc();
                         vx.VariableId = vIds.Id;
                         vx.Index = index;
                         vx.DataStringValue = ind.IdString;
                         vx.IsModified = true;
                         vIds.Values.Add(vx);
                     }
                 }
                 else
                 {
                     var vx = new ValueDesc();
                     vx.Index = index;
                     vx.DataStringValue = ind.IdString;
                     vx.IsModified = true;
                     vIds.Values.Add(vx);
                 }
             }// Ids
             //
             if (vNames != null)
             {
                 if (vNames.Id != 0)
                 {
                     var q = from x in vNames.Values where x.Index == index select x;
                     if (q.Count() > 0)
                     {
                         var vx = q.First();
                         vx.DataStringValue = ind.Name;
                         vx.IsModified = true;
                     }
                     else
                     {
                         var vx = new ValueDesc();
                         vx.VariableId = vNames.Id;
                         vx.Index = index;
                         vx.DataStringValue = ind.Name;
                         vx.IsModified = true;
                         vNames.Values.Add(vx);
                     }
                 }
                 else
                 {
                     var vx = new ValueDesc();
                     vx.Index = index;
                     vx.DataStringValue = ind.Name;
                     vx.IsModified = true;
                     vNames.Values.Add(vx);
                 }
             }// Names
             //
             if (vPhotos != null)
             {
                 if (vPhotos.Id != 0)
                 {
                     var q = from x in vPhotos.Values where x.Index == index select x;
                     if (q.Count() > 0)
                     {
                         var vx = q.First();
                         if (vPhotos.DataType == "string")
                         {
                             vx.DataStringValue = ind.PhotoName;
                         }
                         else
                         {
                             vx.DataStringValue = Convert.ToString(ind.PhotoId);
                         }
                         vx.IsModified = true;
                     }
                     else
                     {
                         var vx = new ValueDesc();
                         vx.VariableId = vPhotos.Id;
                         vx.Index = index;
                         if (vPhotos.DataType == "string")
                         {
                             vx.DataStringValue = ind.PhotoName;
                         }
                         else
                         {
                             vx.DataStringValue = Convert.ToString(ind.PhotoId);
                         }
                         vx.IsModified = true;
                         vPhotos.Values.Add(vx);
                     }
                 }
                 else
                 {
                     var vx = new ValueDesc();
                     vx.Index = index;
                     if (vPhotos.DataType == "string")
                     {
                         vx.DataStringValue = ind.PhotoName;
                     }
                     else
                     {
                         vx.DataStringValue = Convert.ToString(ind.PhotoId);
                     }
                     vx.IsModified = true;
                     vPhotos.Values.Add(vx);
                 }
             }// Names
         }// ind
         List<VariableDesc> oVars = new List<VariableDesc>();
         if (vIds != null)
         {
             oVars.Add(vIds);
         }// ids
         if (vNames != null)
         {
             oVars.Add(vNames);
         }
         if (vPhotos != null)
         {
             oVars.Add(vPhotos);
         }
         if (oVars.Count > 0)
         {
             var xx = await Task.Run<Tuple<bool, Exception>>(() =>
             {
                 return pMan.MaintainsVariableAndValues(oVars);
             });
             if ((xx != null) && (xx.Item2 != null))
             {
                 ShowError(xx.Item2);
             }
             else if ((xx != null) && (xx.Item1) && (xx.Item2 == null))
             {
                 m_main.RefreshVariables();
                
             }
         }// oVars
     }// try
     catch (Exception ex)
     {
         ShowError(ex);
     }
     this.IsBusy = false;
     this.IsModified = false;
 }// CommitChanges