2010-11-23 23 views
0

Silverlight應用程序使用WCF RIA Services連接到SQL Server數據庫。在將大量新記錄插入表格之前,我應該檢查該表格是否包含某個字段中具有某個值的記錄。Silverlight WCF RIA服務。檢查布爾方法的結果

我的服務器端方法在域服務類:

[Invoke] 
    public bool CheckRec(string nameFilter) 
    { 
     bool res = false; 
     if (this.ObjectContext.MyTest.FirstOrDefault(p => p.Name == nameFilter) != null) 
     { 
      res = true; 
     }    
     return res; 
    } 

如何我可以檢查客戶端上的方法,結果呢? 我試圖像下面這樣的方式,但需要一些幫助來正確地實現這一點:

MyTestContext testcontext = new MyTestContext(); 
string tname = savetdlg.TNameTBox.Text; 
testcontext.CheckRec(tname).Completed += (df, fg) => 
       { 
        bool notunique = ?????? // how to get result of the method? 
        if (notunique == true) 
        { 
         //todo if record exists 
        } 
        else 
        { 
         //todo if record doesn't exist 
        }     
       }; 
+0

正交的,但你的支票可以寫成返回this.ObjectContext.MyTest.Any(p => p.Name == nameFilter); – 2010-11-24 02:11:19

+0

根據上下文和檢查原因,您也可以將「if」檢查添加到插入的服務器調用的開頭。當然,如果你需要它是兩個不同的調用(例如,上傳/寫入的記錄數量很大,而且你不需要編寫它們是很常見的),那麼就不要這麼做。 :) – 2010-11-24 02:14:46

回答

2

那麼,在下面的方式進行:

MyTestContext testcontext = new MyTestContext(); 
string tname = savetdlg.TNameTBox.Text; 

testcontext.CheckRec(tname, context_CheckRecCompleted, null); 

void context_CheckRecCompleted(InvokeOperation<bool> op) 
    { 
     bool notunique = op.Value; 
     if (notunique == true) 
     { 
      //todo if record exists 
     } 
     else 
     { 
      //todo if record doesn't exist 
     } 
    }