This should be REALLY simple but I can't figure it out.
I can bind the data to a datagrid like this below...
var context = new DishViewDomainContext();
this.dataGrid1.ItemsSource = context.Restaurants;
context.Load(context.GetRestaurantsQuery());
. That works... But now I just want that collection in a variable that I can loop through collection... This does not seem possible.. I do this and there seems to be nothing there... Im n开发者_如何学编程ot really sure what the 3rd line does.. Its running the domain service method but where is it filling the data at?
var dc = new DomainService1();
IEnumerable<ApplicationLog> collApplicationLog = dc.ApplicationLogs;
dc.Load(dc.GetApplicationLogsQuery());
foreach (ApplicationLog al in collApplicationLog)
{
int? i = al.ApplicationID;
}
Try the following
var collApplicationLog = dc.ApplicationLogs.ToList();
The call to dc.Load(...) is an asynchronous network operation. The call returns immediately, but the data it's supposed to load won't be available until sometime later.
The return value of dc.Load(...) is a LoadOperation object, which has a Completed event. You need to add an event handler to the Completed event so that you will be notified when the data has been loaded and the IEnumerable can be used.
Here's why: Using just an IEnumerable you won't know when the data arrives. The EntitySet that you extract an IEnumerable from also implements INotifyCollectionChanged. That's how XAML databinding to the IEnumerable works - the data binding finds out when the data arrives because it listens to the collection changed notification, then it goes and fetches the data from the enumeration.
Your foreach loop isn't waiting for the data to arrive.
精彩评论