检查表中的值并通过RestAPI更新它

时间:2017-08-02 19:18:52

标签: c# rest

我想检查表格中的值,如果它符合某些条件,请更新它,但我不认为我做得对:

public void UpdateUsertoActive(string email)
    {
        var token = ApiLogin();
        string filter = $"Email_Address = '{email}'";
        var contactstatus = new List<string>
        {"Contact_Status_ID"};
        var activestatus = _ministryPlatformRest.UsingAuthenticationToken(token).Search<MpMyContact>(filter, contactstatus);
        if (activestatus.Equals(2))
        {
            activestatus = _ministryPlatformRest.UsingAuthenticationToken(token).UpdateRecord("dbo.Contacts", GetContactIdByEmail(email), contactstatus);
        }
        else
        {//do nothing}
    }

我想检查Contact_Status_ID; if it is == 2然后将其更改为1,否则不执行任何操作。

这是Contact_Status_ID来自的联系人模型:

namespace MinistryPlatform.Translation.Models
{
    [MpRestApiTable(Name = "Contacts")]
    public class MpMyContact
    {
        public int? Address_ID { get; set; }
        public string Address_Line_1 { get; set; }
        public string Address_Line_2 { get; set; }
        public int? Contact_Status_ID { get; set; }
    }
}

1 个答案:

答案 0 :(得分:2)

通过交换所有代码来查找状态并在过程中发出更新语句,可以使这更简单。像这样的东西。基本上你的代码正在做的是运行查询以查看该行是否存在于该状态。如果是,请运行第二个查询来更新它。只需更新它就可以减少资源消耗。

create procedure UpdateUsertoActive
(
    @EmailAddress varchar(500)
) as 
    set nocount on;

    Update YourTable
    set Contact_Status_ID = 2
    where Contact_Status_ID = 1
        and Email_Address = @EmailAddress