Silverlight应用程序使用WCF RIA Services连接到SQL Server数据库。在将一堆新记录插入表中之前,我应检查该表是否包含其中一个字段中具有特定值的任何记录。
我在域服务类中的服务器端方法:
[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 :(得分: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
}
}