Fill in a collection type property

Asked

Viewed 272 times

7

Good personal evening how to fill a collection property using this type of structure in a class:

public class Order
     {
         public string orderNumber {get; set; }
         public DateTime orderDate {get; set; }
         private DateTime RequiredDate {get; set; }
         private DateTime ShippedDate {get; set; }
         public int CustomerNumber {get; set; }
         public bool status {get; set; }
         public string comments {get; set; }
         private IEnumerable OrderDetail;

         public virtual IEnumerable OrderDetails
         {
             get {return OrderDetail; }
             set {OrderDetail = value; }
         }

         public Order ()
         {
             OrderDetail = new List ();
         }
}

In the Form load event

 private void frmlogin_Load(object sender, EventArgs e)
    {
        try
        {
             myConnection = new MySqlConnection(myConnString);
        myConnection.Open();

        MySqlDataAdapter conexaoAdapter =
            new MySqlDataAdapter("SELECT O.orderNumber,O.orderDate,O.customerNumber, D.productCode,D.quantityOrdered,D.priceEach " + 
                                    "FROM Orders O " +
                                    "INNER JOIN OrderDetails D " + 
                                    "ON O.orderNumber = D.orderNumber ", myConnection);
        conexaoAdapter.Fill(DataTableDatbase);


Continua .....

How to fill this class property private IEnumerable<OrderDetail> orderDetail;

Can someone give some hint or example?

1 answer

1

Well you’re trying to fill a class with a Enumerable, the first step and changes the select , because he brings a single list that would be the fields of his class and within that everything would still have to have the Enumerable , the example below demonstrates how you do it.

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)
        {
            frmlogin_Load(null, null);
        }

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

        private void frmlogin_Load(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = myConnString;
            try
            {
                SqlCommand cmdpapel = new SqlCommand();
                cmdpapel.Connection = conn;
                cmdpapel.CommandType = CommandType.Text;
                cmdpapel.CommandText = "select P.NomePapel as DescrPapel, P.DtInclusao, P.DtInclusao as DtInclusaoUS, P.DtInclusao as DtInclusaoUS2," +
                    "P.IdPapel as IdPapelteste,  P.NomePapel, P.IdPapel, P.IdPapel as IdPapelUS" +
                    " from tb_Papeis P ";

                SqlDataAdapter dapapel = new SqlDataAdapter();
                DataTable dtpapeis = new DataTable();

                dapapel.SelectCommand = cmdpapel;

                conn.Open();
                dapapel.Fill(dtpapeis); // == conexaoAdapter.Fill(DataTableDatbase);

                //---------------------------

                SqlCommand cmdpapelUS = new SqlCommand();
                cmdpapelUS.Connection = conn;
                cmdpapelUS.CommandType = CommandType.Text;
                cmdpapelUS.CommandText = "select *  from tb_PapeisUsuario";

                SqlDataAdapter dapapelUS = new SqlDataAdapter();
                DataTable dtpapeisUS = new DataTable();

                dapapelUS.SelectCommand = cmdpapelUS;

                dapapelUS.Fill(dtpapeisUS); // == conexaoAdapter.Fill(DataTableDatbase);

                //// o seu continua aqui
                List<Order> od = new List<Order>();

                var qrIn = from row in dtpapeis.AsEnumerable()
                           select new Order
                           {
                               orderNumber = row[0].ToString(),
                               orderDate = Convert.ToDateTime(row[1]),
                               requiredDate = Convert.ToDateTime(row[2]),
                               shippedDate = Convert.ToDateTime(row[3]),
                               CustomerNumber = Convert.ToInt32(row[4]),
                               comments = row[5].ToString(),
                               status = true, //row[6].ToString(),
                               OrderDetail = dtpapeisUS.AsEnumerable().ToList().Where(n => Convert.ToInt32(n[1]) == Convert.ToInt32(row[6])).Select(n => n[1]).AsEnumerable(), /// aqui vc caregar os delalhes veja o index da linha para o Where
                           };

                // aqui vc tem a lista e pode chama sua classe ou add a lista a ela.
                var Lista = qrIn.ToList();

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

    public class Order
    {
        public string orderNumber { get; set; }
        public DateTime orderDate { get; set; }
        public DateTime RequiredDate { get; set; }
        public DateTime requiredDate { get; set; }
        public  DateTime shippedDate { get; set; }
        public int CustomerNumber { get; set; }
        public bool status { get; set; }
        public string comments { get; set; }
        public IEnumerable OrderDetail;
    }
}

Change the Selects with their "Orders" tables," Orderdetails" and fields...

Browser other questions tagged

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