Transfer of List from one class to another

1

I want to treat a list in a method from another class and then return the treated list. I would like to know how to transfer this list to the other class.

I did as follows:

Main class:

DataTable result = new DataTable();
VerificaJuridico vj = new VerificaJuridico();


List<DataRow> rows = result.Rows.Cast<DataRow>().ToList();

            List<DataRow> list = new List<DataRow>(result.Select());
             vj.ChecaJuridico(list);

Second Class:

namespace Comunicacao
{

public class VerificaJuridico
{

public void ChecaJuridico<T>(List<T> lista)
{
    foreach (var  t in lista)
    {        
    }
}
}
}

When running the program, the main class transfers the value, but the second class running the method does not bring anything, what can it be? Is something wrong with the transfer?

    
asked by anonymous 03.12.2015 / 14:36

1 answer

1

I do not know how you're loading your DataTable result , but I'll give you an example using the sql server database for this. See below ..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1tiraduvidas
{
    public partial class ClasseComLista : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Teste_Load(null, null);
        }

        public string myConnString
        {
            get
            {
                return "Server=.\SQLEXPRESS;Database=BancoModelo;User ID=sa;Password=814485";
            }
        }

        private void Teste_Load(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = myConnString;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select P.NomePapel , P.DtInclusao from tb_Juridico P ";

                SqlDataAdapter sqla = new SqlDataAdapter();
                DataTable Result = new DataTable();

                sqla.SelectCommand = cmd;

                conn.Open();
                sqla.Fill(Result);

                List<Juridic> od = new List<Juridic>();

                var qrIn = from row in Result.AsEnumerable()
                           select new Juridic
                           {
                               Nome = row[0].ToString(),
                               DtInclusao = Convert.ToDateTime(row[1]),
                           };

                var Lista = qrIn.ToList();

                VerificaJuridico vj = new VerificaJuridico();
                vj.ChecaJuridico(Lista);

            }
            finally
            {
                conn.Close();
            }
        }
    }

    public class VerificaJuridico
    {
        public void ChecaJuridico<T>(List<T> lista)
        {
            // se for uma lista generica (ChecaJuridico<T>(List<T>) vc vai ter que acessa os valores atraves da propriedade
            var properties = typeof(T).GetProperties();
            foreach (var item in lista)
            {
                foreach (var property in properties)
                {
                    var name = property.Name;
                    var value = property.GetValue(item, null);
                }
            }
        }

        // ou assim vc pode fazer assim 
        public void ChecaJuridico(List<Juridic> lista)
        {
            foreach (var t in lista)
            {
                var nome = t.Nome;
                var DtInclusao = t.DtInclusao;
            }
        }
    }

    public class Juridic
    {
        public string Nome { get; set; }
        public DateTime DtInclusao { get; set; }
    }
}
    
03.12.2015 / 16:39