2
I would like a help in passing values to my property
private IEnumerable<OrderDetail> orderDetail;
.
I would like to fill it in, but I have already looked for some examples and I have not found anything with this situation. I ask for the help of colleagues if I can show some way to carry out these procedures without having to fill two Datatable to make Inner Join with the Linq.
I will present the structure of the Classes below and how I am performing the search in the Database.
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> orderDetail;
public virtual IEnumerable<OrderDetail> OrderDetails
{
get { return orderDetail; }
set { orderDetail = value; }
}
public Order()
{
orderDetail = new List<OrderDetail>();
}
//No formulário do Evento Load
private void Form1_Load(object sender, EventArgs e)
{
myConnection = new MySqlConnection(myConnString);
myConnection.Open();
MySqlDataAdapter conexaoAdapter =
new MySqlDataAdapter("select O.*, D.* " +
"from Orders O " +
"INNER JOIN OrderDetails D " +
"ON O.orderNumber = D.orderNumber ", myConnection);
conexaoAdapter.Fill(DataTableDatbase);
.....
}
I tried to use with the LINQ but it did not work this structure below the error:
Error 1 Cannot implicitly Convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.Ienumerable'. An Explicit Conversion exists (are you Missing a cast?) D: Samples Projects Pizzaria Testelinq LINQ_TESTE2 Form1.Cs 47 42 LINQ_TESTE2
IEnumerable<Order> queryNamesScores =
from Listorder in DataTableDatbase.Tables["Orders"].AsEnumerable()
select new Order()
{
orderNumber = DataTableDatbase.Tables[0].Rows[0]["orderNumber"].ToString(),
OrderDetails = (from scoreAsText in DataTableDatbase.Tables["OrderDetails"].AsEnumerable()
select scoreAsText).ToList()
};
Does anyone have any example, hint or structure to perform this procedure of filling the collection type property of my class?
I don’t know if the code has any more problems but the error is due to which Orderdetails is stated as
IEnumerable<OrderDetail>
and you’re assigning him aList
. Retire.ToList()
of.... select scoreAsText).ToList()
– ramaral
But like if you look at the Order class I have a List property... So how would you mount the above example?
– ts.analista
@ramaral I assure you that the problem is not in assigning List<Orderdetail> to Ienumerable<Orderdetail>, after all the class List<T> implements Ienumerable<T> https://msdn.microsoft.com/pt-br/library/6sh2ey19(v=vs.110). aspx this is totally possible in object orientation.
– user26552
@Murilo You are right, was taken by the error text that says you can not convert the two types
– ramaral
I understand, it’s the first thing I would think too, but I don’t have a solution either.
– user26552