Repeat my question, old text below
Since I still hope for an answer, I would like to repeat my question. Image I have a graphical interface with two lists, one of which shows a list of all the records in the tblOrders database and the other that shows the elements in each order.
I can use Linq2sql or EF to get all orders from the database, for example:
using (DataClasses1DataContext DataContext = new DataClasses1DataContext()) ListOfOrders = DataContext.tblOrder.ToList();
I can display this order in a list or datagridview . Then, by choosing one of the tasks, I want to access the collection of objects from the tblItems table. I can do it like this:
ListOfOrders.First().tblItems.toList();
Also, I cannot, because I need a DataContext that has been deleted. This makes sense in some way, because I cannot guarantee that there are no new items that were added to this order, since I received the ListOfOrders. So ideally I would like to check if there has been an addition to a tblOrder.tblItems collection ListOfOrders. So ideally I would like to check if there has been an addition to a tblOrder.tblItems ListOfOrders. So ideally I would like to check if there has been an addition to a tblOrder.tblItems and only recently retrieved this collection from the server, if necessary.
My model is supposed to be a little more complicated: it consists of orders consisting of parts that consist of tasks. Therefore, in order to evaluate the progress of each order, I have to get all the parts belonging to the order, and for each of them I have to see how many tasks have been completed. In a database with 200 tasks, each of which contains from 1 to 10 parts, this simply slows down my program in terms of responsiveness ...
Can anybody help me?
Original question
I found many questions regarding the DataContext , but have not yet found a solution to my problem. If I do the following:
using (DataClasses1DataContext DataContext = new DataClasses1DataContext()) ListOfOrders = DataContext.tblOrder.ToList();
This gives me a list of objects in the tblOrder table. But now I want to do this:
DataTable Orders = new DataTable(); Orders.Columns.Add("Order-ID", typeof(int)); Orders.Columns.Add("Order-Name", typeof(string)); Orders.Columns.Add("Order-Items", typeof(string)); dataGridView1.DataSource = Orders; foreach (tblOrder Order in ListOfOrders) { var newRow = Orders.NewRow(); newRow["Order-ID"] = Order.orderID; newRow["Order-Name"] = Order.orderName; newRow["Order-Items"] = string.Join(", ", Order.tblItem.Select(item=> item.itemName).ToList());
And I cannot, because access to all the objects in the tblItem table associated with foreign key orders does not seem to be preserved.
What works:
DataClasses1DataContext DataContext = new DataClasses1DataContext(); ListOfOrders = DataContext.tblOrder.ToList(); DataTable Orders = new DataTable(); Orders.Columns.Add("Order-ID", typeof(int)); Orders.Columns.Add("Order-Name", typeof(string)); Orders.Columns.Add("Order-Items", typeof(string)); dataGridView1.DataSource = Orders; foreach (tblOrder Order in ListOfOrders) { var newRow = Orders.NewRow(); newRow["Order-ID"] = Order.orderID; newRow["Order-Name"] = Order.orderName; newRow["Order-Items"] = string.Join(", ", Order.tblItem.Select(item=> item.itemName).ToList()); (dataGridView1.DataSource as DataTable).Rows.Add(newRow); } DataContext.Dispose();
But, as I understand it, this is undesirable.
EDIT
I extended my example to Controller-View-Pattern.
using System.Collections.Generic; using System.Linq; namespace TestApplication { class Controller { private List<tblOrder> _orders; public IList<tblOrder> Orders { get { return _orders; } } public Controller() { using (var DataContext = new DataClasses1DataContext()) { _orders = DataContext.tblOrder.ToList(); } } } }
Now the view now receives Orders from the controller:
using System.Data; using System.Linq; using System.Windows.Forms; namespace TestApplication { public partial class Form1 : Form { public Form1() { InitializeComponent(); Controller controller = new Controller(); DataTable Orders = new DataTable(); Orders.Columns.Add("Order-ID", typeof(int)); Orders.Columns.Add("Order-Name", typeof(string)); Orders.Columns.Add("Order-Items", typeof(string)); dataGridView1.DataSource = Orders; foreach (tblOrder Order in controller.Orders) { var newRow = Orders.NewRow(); newRow["Order-ID"] = Order.orderID; newRow["Order-Name"] = Order.orderName; newRow["Order-Items"] = string.Join(", ", Order.tblItem.Select(item=> item.itemName).ToList()); (dataGridView1.DataSource as DataTable).Rows.Add(newRow); } } } }
Unfortunately, the problem remains the same ...