Lists with Linq asp.net

0

I have been reading about the lists in asp.net and how it works, I currently have a web application where I do a lot of queries to the database and therefore the performance is not the desired one.

I have done several exercises introducing the data to the lsita manually

example:

ClaimedVehicles MyVehicles = new ClaimedVehicles(1, "hola", DateTime.Today, "MyModel", 1, "asas", "asas");
    ClaimedVehicles MyVehicles1 = new ClaimedVehicles(1, "hola", DateTime.Today, "P3", 1, "asas", "asas");
    ClaimedVehicles MyVehicles2 = new ClaimedVehicles(2, "hola", DateTime.Today, "P3", 1, "asas", "asas");

    List<ClaimedVehicles> MyListOFVehicles = new List<ClaimedVehicles>();
    MyListOFVehicles.Add(MyVehicles);
    MyListOFVehicles.Add(MyVehicles1);
    MyListOFVehicles.Add(MyVehicles2);

    MyListOFVehicles.Add(new ClaimedVehicles(1, "hola", DateTime.Today, "hi", 1, "asas", "asas"));

but I can not understand how I have to do with a database query.

I currently have a database in access, I've been trying to get the data from the table users

public class Users
    {
        public int ID { get; set; }
        public String NAME { get; set; }
        public int TEAM { get; set; }
        public int ROL_ID { get; set; }

        public Users(
            int id,
            String name,
            int team,
            int rol_id
            )
        {
            this.ID = id;
            this.NAME = name;
            this.TEAM = team;
            this.ROL_ID = rol_id;
        }
        private Users() { }
    }

and on my list something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace ToolTrackSystem.List
{
    public class ListUsers
    {
        String myConnectionString = @"C:\Users\gutiece\Desktop" + "tool_track.accdb";
        static void Main(string[] args)
        {
            List<Data.Users> newUserList = new List<Data.Users>();
            using (OleDbConnection connection = new OleDbConnection())
            {
                using (OleDbCommand command = new OleDbCommand())
                {
                    try
                    {
                        string Query = @"SELECT id, who, payroll_number, name, ou, pool, team, rol_id  FROM users  ORDER BY id DESC AS Users";

                        OleDbCommand = Query; <-- no se que siga y me marca error


                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
        }
    }
}

I want to have the data in a list and serialize it to an XML file to have a quicker reading of all the data ... my problem and question is that I do not know what to do for the query

How do I make the list with a realization of the query?

    
asked by Cesar Gutierrez Davalos 16.06.2017 в 16:42
source

2 answers

0

I'll let you fill a list from the database

sql query:

  ALTER PROCEDURE [dbo].[spListar_ConsultaConfigOtrosBcos]
AS    
BEGIN    
    SELECT cID Valor,glosaMovimiento Texto, glosaCartola Cartola, glosaBanco Banco, codBanco codigoBco 
    FROM ConfigOtrosBcos
END

this is my my class to capture the data from the BD:

   public class TextoValor
    {
        [ResultField("Texto")]
        public string Texto { get; set; }
        [ResultField("Valor")]
        public string Valor { get; set; }
        [ResultField("Banco")]
        public string Banco { get; set; }
        [ResultField("codigoBco")]
        public string codigoBco { get; set; }
        [ResultField("Cartola")]
        public string Cartola { get; set; }

    }

Here I bring the sql query to a list:

 public static List ConsultaConfigOtrosBcos()
        {

            Result Resultado = null;
            Base.StoreBase StoreBase = new Base.StoreBase();
            List Listado = new List();
            SpExecutor SpExecutor = SpExecutor.Create("spListar_ConsultaConfigOtrosBcos", "EuroAmerica.PortalDePagos");

            SpExecutor.GenerateExceptions = true;

            StoreBase.RegistrarEventoInicioEjecucionSp(SpExecutor);

            try
            {
                StoreBase.GestorDeReintentos.Ejecutar(() =>
                {
                    Resultado = SpExecutor.Execute();
                    StoreBase.RegistrarEventoFinEjecucionExitosa(Resultado);

                });
                if (Resultado.IsOk)
                {
                    Resultado.PopulateList(Listado, new Clases.TextoValor());
                }

                return Listado;
            }
            catch (Exception Exception)
            {
                return new List();
            }
        }

and here I start to work the list (at the end of this part I work with linq):

   public static List DataTableToListCartolaOtrosBcos(DataTable Listado, int cabID,string  cod_banco, string cod_sucursal)
        {
            List ListadoRendicion = new List();
            EuroAmericaPortalDePagos.FrontOffice.Clases.Rendicion Linea = new EuroAmericaPortalDePagos.FrontOffice.Clases.Rendicion();
            try
            {
                List ListaGlosas = ConsultaConfigOtrosBcos();
                foreach (DataRow oDR in Listado.Rows)
                {

                    Linea = new EuroAmericaPortalDePagos.FrontOffice.Clases.Rendicion();

                    long Cargo = long.Parse(oDR["Cargo ($)"].ToString() == "" ? "0" : oDR["Cargo ($)"].ToString());
                    long Abono = long.Parse(oDR["Abono ($)"].ToString() == "" ? "0" : oDR["Abono ($)"].ToString());

                    Linea.ID = long.Parse(oDR["Linea"].ToString());
                    Linea.cabID = Convert.ToInt64(cabID.ToString().Trim());
                    Linea.det_fecha = DateTime.ParseExact(oDR["Fecha"].ToString().Trim().Replace("-", "/"), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    Linea.det_tipo_movimiento = Cargo > 0 ? "C" : "A";
                    Linea.det_codigo = "0";
                    Linea.det_glosa_codigo = oDR["Descripción"].ToString();
                    Linea.det_identificador_ser = "";
                    Linea.det_num_docto = long.Parse(oDR["Serial Documento"].ToString() == "" ? "0" : oDR["Serial Documento"].ToString());
                    Linea.det_monto = Cargo > 0 ? Cargo : Abono;
                    Linea.det_codigo_sucursal = 0;
                    Linea.det_glosa_sucursal = cod_sucursal;
                    Linea.det_cuponID = 0;
                    Linea.det_banco = cod_banco;
                    Linea.det_fechaCrea = DateTime.Now;
                    var glosas = ListaGlosas.Where(e => e.Cartola == Linea.det_glosa_codigo && e.codigoBco == Linea.det_banco).ToList();
                    if (glosas.Count > 0)
                    {
                        Linea.det_glosa_codigo = glosas.FirstOrDefault().Texto;
                    }                      

                   ListadoRendicion.Add(Linea);
                }



                //List revision = new List();
            }
            catch (Exception)
            {
                return new List();
            }
            return ListadoRendicion;
        }
    
answered by 16.06.2017 / 17:35
source
0

You have to execute the query:

OleDbCommand cmd = new OleDbCommand("SELECT id, who, payroll_number, name, ou, pool, team, rol_id  FROM users  ORDER BY id DESC AS Users");
OleDbDataReader reader = cmd.ExecuteReader();

while (reader.Read())
        {
            Data.Users li = new Data.Users();
            li.Add(reader.GetString(1));
            li.Add(reader.GetString(2));
            li.Add(reader.GetString(3));
            li.Add(reader.GetString(4));
            ...
            newUserList.Add(li);
        }
    
answered by 16.06.2017 в 16:58