0

我试图避免两次调用数据库。我需要检查是否存在记录,如果存在,则用数据填充我的视图。我有以下代码:

        if (Presenters.PayeePresenter.GetByID(id) != null)
        {
            view = BLL.Presenters.PayeePresenter.GetByID(id);

            msg.Success = true;
            msg.Text = "Record Found";
        }

我怎样才能对数据库进行最少的调用?

4

1 回答 1

6

将结果存储在变量中,并在分配属性之前检查其是否为空。

var obj = Presenters.PayeePresenter.GetByID(id); //Assuming this is database method call
if (obj!= null)
{
   //use obj.Properties to fill custom object or any additional logic
   msg.Success = true;
   msg.Text = "Record Found";
}
于 2013-03-05T01:20:33.527 回答