如何验证字段项的存在并更改其值?

how can I verify the existence of a field item and change its value?

我必须检查该项目是否有“训练中”字段,如果该字段存在且不同于 1,我必须将其设置为 1。

var field = "In Allenamento";
        var value = "0";
        var edited = (field = "1");

       if (field != null && field == value)
        {
            field = "1";

        }

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                item.Editing.BeginEdit();
                item.Fields[field].Value = field;
                item.Editing.EndEdit();
                log.AppendLine(item.ID + "edited");
            }
       
    }

知道自己写了废话,求支持

  1. 根据字段名称(“In Allenamento”或“In Training”)
  2. 如果字段为空,则表示该字段不存在该项目
  3. 检查它的值是否不是“1”
  4. 开始编辑
  5. 更新值
  6. 结束编辑
using (new Sitecore.SecurityModel.SecurityDisabler())
{
    var inTrainingField = item.Fields["In Allenamento"];
    if (inTrainingField != null && inTrainingField.Value != "1")
    {
        item.Editing.BeginEdit();
        item.Fields["In Allenamento"].Value = "1";
        item.Editing.EndEdit();
        log.AppendLine(item.ID + "edited");
    }
}