Am trying to populate a datagrid with contents of a List, but afterwards the list contains the right amount of items, all with the same data row. For example, when press = 8 there are three different batches returned in SQL, but in C# there is one batch returned three times. I'm not sure if the problem is in my query or foreach loop - but I used a messagebox to display the current ScheduleNr during the loop and it was always the same.
List<BatchList> myBatches = new List<BatchList>();
BatchList batch;
var db = new BatchListContext();
var batchQuery = from b in db.BatchLists
where b.Press == press
orderby b.ExtrDate, b.BatchNr
select b;
if (batchQuery.Count() == 0)
{
MessageBox.Show("Error loading batches", "Error", MessageBoxButton.OK);
return false;
}
foreach (var batchItem in batchQuery)
{
batch = new BatchList();
batch.ScheduleNr = batchItem.ScheduleNr;
batch.BatchNr = batchItem.BatchNr;
batch.Press = batchItem.Press;
batch.ExtrDate = batchItem.ExtrDate;
batch.NoOrders = batchItem.NoOrders;
myBatches.Add(batch);
MessageBox.Show(String.Format("{0}", batchItem.ScheduleNr.ToString())); //Always shows same ScheduleNr
}
Any advice would be greatly appreciated where I might have made a fundamental mistake.
Many thanks.
//EDIT BatchList is a SQL Server View that if queried in SSMS as follows:
SELECT * FROM BilletPlanner.BatchList WHERE Press = 8
returns:
ScheduleNr BatchNr Press ExtrDate NoOrders
10035620 2 8 2015-06-22 32
10035629 3 8 2015-06-22 22
10035631 4 8 2015-06-23 32
But the DataGrid I am using in C# shows the following (crude copy as cannot paste image currently)
ScheduleNr BatchNr Press ExtrDate NoOrders
10035620 2 8 22/06/2015 32
10035620 2 8 22/06/2015 32
10035620 2 8 22/06/2015 32
db.BatchListsreturnsBatchList, then you don't really need to loop through the query and copy an identicalBatchList, you could just add aToList()to the end of your query (wrap it in brackets and add it at the end, i.e.myBatches = (from b in db.batchLists...).ToList()