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<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);
        }