2010-04-19 26 views
1

我是WCF & RIA服務的新手,看起來像是一個基本問題。我有多次在我的Silverlight 4 RC應用程序中從我的數據源窗口拖放到我的表單並從數據庫返回信息。但是,我需要查詢數據庫以獲取其他信息(構建報告)當我嘗試使用以下代碼時,我沒有收到任何錯誤,但我也沒有從服務中獲得任何信息。在Silverlight 4 RC應用程序中手動連接DomainContext並加載數據

//Global 
public UMSDomainContext _umsDomainContext = new UMSDomainContext(); 

//In the Initialization portion 
_umsDomainContext.Load(_umsDomainContext.GetUMOptionsQuery()); 
//Queries 
var name = from n in _umsDomainContext.UMOptions 
           select n.DistrictName; 

       var street1 = from c in _umsDomainContext.UMOptions 
           select c.Address1; 

       var street2 = from c in _umsDomainContext.UMOptions 
           select c.Address2; 

       var city = from c in _umsDomainContext.UMOptions 
           select c.City; 

       var zip = from c in _umsDomainContext.UMOptions 
           select c.Zip; 

我正在調用當前的附加參考。

using System.Linq; 
using System.ServiceModel.DomainServices.Client; 
using System.ComponentModel.DataAnnotations; 
using MTT.Web; 

回答

2

答案很簡單。數據加載時即刻被查詢。雖然有些應用程序會在程序上處理這個問題,但似乎Silverlight在繼續之前並未等待數據加載。所以我做了以下工作:

public void LoadCustomerInformation() 
{ 
    //Load the Query 
    var loadOperation = _umsDomainContext.Load<UMOption>(_umsDomainContext.GetUMOptionsQuery()); 
    //Create a event handler to run after the data is fully loaded. 
    loadOperation.Completed += new EventHandler(loadOperation_Completed); 
} 

void loadOperation_Completed(object sender, EventArgs e) 
     { 
      var name = (from n in _umsDomainContext.UMOptions 
         select n.DistrictName).First(); 

      var street1 = from c in _umsDomainContext.UMOptions 
          select c.Address1; 

      var street2 = from c in _umsDomainContext.UMOptions 
          select c.Address2; 

      var city = from c in _umsDomainContext.UMOptions 
         select c.City; 

      var zip = from c in _umsDomainContext.UMOptions 
         select c.Zip; 

      //Other code to work with the data, etc... 
     } 
相關問題