private void GuardaCambiosReporte(appreportes model)
        {
            appreportesRepository _appreportesRepository = new appreportesRepository();
              if (model.Id_centro == 0)
            model.Id_centro = null;

              if (model.Id_app_reporte == 0)
            _appreportesRepository.Create(model);
              else
              {
            _appreportesRepository.Edit(model);
            EditCurrentQuery(model);
              }
              string[] split = (model.selectedFiedls == null ? new string[0] : model.selectedFiedls.Split(new Char[] { ',' }));
              string[] splitWhere = (model.selectedFiedlsWhere == null ? new string[0] : model.selectedFiedlsWhere.Split(new Char[] { ',' }));
              string[] splitOrder = (model.selectedFiedlsOrder == null ? new string[0] : model.selectedFiedlsOrder.Split(new Char[] { ',' }));

              ///Guarda los campos de la instrucci[on select
              for (int i = 0; i < split.Length; i++)
              {
            int start = split[i].IndexOf("(");
            int end = split[i].IndexOf(")");
            string result = string.Empty;
            string srfuncion = string.Empty;

            if (start != -1 && end != -1)
            {
              result = split[i].Substring(start + 1, end - start - 1);
              srfuncion = split[i].Substring(0, start);
            }
            appReport_Fields _appReport_Fields = new appReport_Fields();
            _appReport_Fields.Id_app_reporte = model.Id_app_reporte;

            if (result != string.Empty)
            {
              string[] split2 = split[i].Trim().Split(new Char[] { '|' });
              if (split2.Length == 1)
              {
            _appReport_Fields.Campo = result.Trim();
            _appReport_Fields.Funcion = srfuncion.Trim();
              }
              else
              {
            _appReport_Fields.Campo = result.Trim();
            _appReport_Fields.Funcion = srfuncion.Trim();
            _appReport_Fields.VerTotal = 1;
              }

            }
            else
            {
              string[] split2 = split[i].Trim().Split(new Char[] { '|' });
              if (split2.Length == 1)
            _appReport_Fields.Campo = split[i].Trim();
              else
              {
            _appReport_Fields.Campo = split2[0].Trim();
            _appReport_Fields.VerTotal = 1;
              }
            }

            _appReport_Fields.Orden = i + 1;
            model.appReport_Fields.Add(_appReport_Fields);
              }

              List<appReport_Fields> campoIdCentro = model.appReport_Fields.Where(e => e.Campo == "Id_centro").ToList();

              //if (campoIdCentro.Count() == 0)
              //{
              //  if (model.tieneCentro == true)
              //  {
              //    model.appReport_Fields.Add(
              //    new appReport_Fields
              //    {
              //      Campo = "Id_centro"
              //    });

              //  }
              //}
              ///Guarda los campos condicionales
              for (int i = 0; i < splitWhere.Length; i++)
              {
            appReport_where _appReport_where = new appReport_where();

            _appReport_where.Id_app_reporte = model.Id_app_reporte;
            string[] splitWhereField = splitWhere[i].Split(new Char[] { '.' });
            _appReport_where.Campo = splitWhereField[1].Trim();
            _appReport_where.Condicion = splitWhereField[2].Trim();
            _appReport_where.TipoParametro = splitWhereField[3].Trim();
            _appReport_where.Operacion = splitWhereField[0].Trim();

            if (splitWhereField.Length == 5)
              _appReport_where.Ultimo_valor = splitWhereField[4].Trim();

            model.appReport_where.Add(_appReport_where);
              }
              for (int i = 0; i < splitOrder.Length; i++)
              {
            appReport_Order _appReport_Order = new appReport_Order();
            _appReport_Order.Id_app_reporte = model.Id_app_reporte;
            string[] splitOrderField = splitOrder[i].Trim().Split(new Char[] { ' ' });
            _appReport_Order.Campo = splitOrderField[0].Trim();

            if (splitOrderField.Length > 1)
              _appReport_Order.Tipo_orden = splitOrderField[1].Trim();
            else
              _appReport_Order.Tipo_orden = "ASC";

            _appReport_Order.Orden = i + 1;
            model.appReport_Order.Add(_appReport_Order);

            appReport_Fields _appReport_Fields2 = model.appReport_Fields.SingleOrDefault(e => e.Campo == _appReport_Order.Campo);

            if (_appReport_Fields2 == null)
            {
              appReport_Fields _appReport_Fields = new appReport_Fields();
              _appReport_Fields.Campo = splitOrderField[0].Trim();

              int countFields = 0;

              if (model.appReport_Fields != null)
            countFields = model.appReport_Fields.Count() + 1;
              else
            countFields = 1;

              _appReport_Fields.Orden = countFields;

              model.appReport_Fields.Add(_appReport_Fields);
            }
              }

              _appreportesRepository.Edit(model);

              _appreportesRepository.Commit();
              _appReport_whereRepository.Commit();
              _appReport_OrderRepository.Commit();
              _appReport_FieldsRepository.Commit();
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the appReport_Fields EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToappReport_Fields(appReport_Fields appReport_Fields)
 {
     base.AddObject("appReport_Fields", appReport_Fields);
 }
        private void EditCurrentQuery(appreportes model)
        {
            string[] split = (model.selectedFiedls == null ? new string[0] : model.selectedFiedls.Split(new Char[] { ',' }));
              string[] splitWhere = (model.selectedFiedlsWhere == null ? new string[0] : model.selectedFiedlsWhere.Split(new Char[] { ',' }));
              string[] splitOrder = (model.selectedFiedlsOrder == null ? new string[0] : model.selectedFiedlsOrder.Split(new Char[] { ',' }));
              List<appReport_Fields> listField = model.appReport_Fields.ToList();
              List<appReport_where> listWhere = model.appReport_where.ToList();
              List<appReport_Order> listOrder = model.appReport_Order.ToList();

              foreach (appReport_Fields item in listField)
              {

            appReport_Fields _appReport_Fields = new appReport_Fields();
            _appReport_Fields = model.appReport_Fields.SingleOrDefault(e => e.Campo == item.Campo);

            if (_appReport_Fields != null)
              model.appReport_Fields.Remove(_appReport_Fields);

            _appReport_FieldsRepository.Delete(_appReport_Fields.Id_report_field);
              }

              foreach (appReport_where item in listWhere)
              {

            appReport_where _appReport_where = new appReport_where();
            _appReport_where = model.appReport_where.SingleOrDefault(e => e.Campo == item.Campo);

            if (_appReport_where != null)
              model.appReport_where.Remove(_appReport_where);

            _appReport_whereRepository.Delete(_appReport_where.Id_report_where);
              }

              foreach (appReport_Order item in listOrder)
              {

            appReport_Order _appReport_Order = new appReport_Order();
            _appReport_Order = model.appReport_Order.SingleOrDefault(e => e.Campo == item.Campo);

            if (_appReport_Order != null)
              model.appReport_Order.Remove(_appReport_Order);

            _appReport_OrderRepository.Delete(_appReport_Order.Id_report_order);
              }
        }
 /// <summary>
 /// Create a new appReport_Fields object.
 /// </summary>
 /// <param name="id_report_field">Initial value of the Id_report_field property.</param>
 public static appReport_Fields CreateappReport_Fields(global::System.Int32 id_report_field)
 {
     appReport_Fields appReport_Fields = new appReport_Fields();
     appReport_Fields.Id_report_field = id_report_field;
     return appReport_Fields;
 }