Transfer List from one class to another

Asked

Viewed 542 times

1

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

I did it this way:

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 brings nothing, what can it be? There’s something wrong with the transfer ?

  • What value list receives on this line: List<DataRow> list = new List<DataRow>(result.Select());

  • You should not pass the Rows variable in the method?

  • In reality they are fields of a datatable which I have transferred to a list, i.e., result is a datatable. The talk for List was ok, I just can’t get the list in the second class. It comes empty.

  • It is not empty already from the list?

  • Truth ... the first routine is empty... How could I transfer a query already in Datatable and transfer it to a List?

  • Where are you carrying the result ?

  • You’re using some database for that?

Show 2 more comments

1 answer

1


I don’t know how you’re carrying yours 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; }
    }
}
  • Perfect Marconcilio, that’s what I wanted, I just can’t get the list item as described below. public void Checajuridico<T>(List<T> list) { foreach (var t in list) { } }

  • I changed the answer....

  • @Joeliasandrade, see the best way to treat your list.

  • Even Marconcílio, gave very sure the capture of the data of the List.Where I must validate you?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.