I am using RIA Services with Silverlight and using following code to load a collection in DataItemCollection.
My issue is the the LoadOperation runs it returns with 0 rows and then after some time, It again gains the control in debugger and then runs the for loop and then gives the correct count.
Hence, it seems it's asynchronous. How can i get it synchronously so that it gives me correct count while returning the data?
ReportingCategoryContentAssociationContext _ReportingCategoryContentAssociationContext = new ReportingCategoryContentAssociationContext();
DataItemCollection lstdt = new DataItemCollection();
LoadOperation loadopt = _ReportingCategoryContentAssociationContext.Load(_ReportingCategoryContentAssociationContext.GetReportingContentScoreByCategoryQuery());
loadopt.Completed += (s, args) =>
{
if (!loadopt.HasError)
{
DataItem dtitem = null;
foreach (GetReportingCategoriesContentScore_Result Lkt in ((LoadOperation<GetReportingCategoriesContentScore_Result>)s).Entities)
{
dtitem = new DataItem();
dtitem.ReportingCategoryID = Lkt.CategoryID;
dtitem.ParentCategoryID = Lkt.ParentCategoryID;
dtitem.CategoryTitle = Lkt.CategoryTitle;
lstdt.Add(dtitem);
}
}
};
All RIA Services calls in Silverlight are Asynchronous.
Your loadopt.Completed += (s, args) =>
code is just an anonymous async callback. Single-step debugging the load is fooling you into thinking the code is happening in sequence.
You can only operate on the data in the completed event callback.