Fill in a collection type property

7

Good evening folks how to populate a collection type 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's 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 anyone pass any tips or examples?

    
asked by anonymous 20.06.2015 / 03:46

1 answer

1

Well you are trying to fill a class with a Enumerable , the first step and change the select , because it brings a unique list that would be the fields of your class and within that everything would still have to have Enumerable , the example below demonstrates how you can do this.

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 your "Orders", "OrderDetails" tables and fields ...

    
25.11.2015 / 20:27