首页 > 解决方案 > 如果字段 2 为空,C# 使用字段 1 的值

问题描述

我正在使用 for each 循环遍历一组记录,并进行简单检查以确保将好的数据插入到数据库表中。

有时数据集可能缺少 LegistarID 值,我需要在我的代码中做的更改是添加对 LegistarItem 的检查,

如果缺少 LegistarID 的值,但没有 AgendaItem 值,则将 AgendaItem 的值分配给 LegistarID

如果缺少 LegistarId,并且也没有 AgendaItem 值,则向用户返回一条消息,让他们知道这些值需要存在于他们尝试导入的数据集中。

我知道这听起来并不复杂,但我很难成功地进行此更改。如果可能的话,我需要一些帮助。

这是我目前拥有的代码:

if (ModelState.IsValid)
{
    using (Etities db = new Entities())
    {
        foreach (var i in meeting)
        {
            if (i.MeetingID == 0)
            {
                message = string.Format("This file is missing the Meeting ID value of at least 1 record. \n Verify that the data you are trying to upload meets the criteria, and then try to upload your file again.", i.MeetingID);
                return new JsonResult { Data = new { status = status, message = message } };
            }
            else
            {
                // development
                var compositeKey = db.MeetingAgenda.Find(i.MeetingID, i.AgendaItem);                               
                if (compositeKey == null)
                {
                    // Add new
                    // development
                    db.MeetingAgenda.Add(i);
                    //
                }
                else
                {
                    // Serves as an update, or addition of a previously imported dataset
                    db.Entry(compositeKey).CurrentValues.SetValues(i.MeetingID);
                    db.Entry(compositeKey).State = EntityState.Modified;
                }
            }
        }
        db.SaveChanges();
        status = true;
    }
}
else
{
    message = string.Format("Please, verify that the file you are trying to upload is correctly formatted, and that the data it contains, meets the expected criteria, then click the upload button again. \n Thank you!");
    return new JsonResult { Data = new { status = status, message = message } };
}

我认为我需要的部分代码是这样的:

else if (i.LegistarID == 0 and i.AgendaItem != 0)
{
    i.LegistarID = i.AgendaItem                             
}

我只是不确定如何在当前代码中放置它。

标签: c#asp.net

解决方案


我会在返回结果之前检查所有行。

if (ModelState.IsValid) {
    var errors = new List<string> ();
    var rowCounter = 1;
    using (Etities db = new Entities ()) {
        foreach (var i in meeting) {
            if (i.MeetingID == 0) {
                // Let the user know this row is bad 
                errors.Add ($"Row {rowCounter}: This file is missing the Meeting ID. Verify that the data you are trying to upload meets the criteria, and then try to upload your file again.");
            }
            // Check if LegistarID is missing
            if (i.LegistarID == 0) {
                // Check if Agenda Item is present
                if (i.AgendaItem == 0) {
                    errors.Add ($"Row {rowCounter}: Meeting has no LegistarID and no Agenda Item. Please check data");                    
                } else {
                    i.LegistarID = i.AgendaItem
                }
            }

            // development
            var compositeKey = db.MeetingAgenda.Find (i.MeetingID, i.AgendaItem);
            if (compositeKey == null) {
                // Add new
                // development
                db.MeetingAgenda.Add (i);
                //
            } else {
                // Serves as an update, or addition of a previously imported dataset
                db.Entry (compositeKey).CurrentValues.SetValues (i.MeetingID);
                db.Entry (compositeKey).State = EntityState.Modified;
            }
            rowCounter++;
        }
        // If there are errors do not save and return error message
        if (errors.Count > 0) {            
            return new JsonResult { Data = new { status = false, message = string.Join ("\n", errors) } };
        }
        db.SaveChanges ();
        status = true;
    }
} else {
    message = string.Format ("Please, verify that the file you are trying to upload is correctly formatted, and that the data it contains, meets the expected criteria, then click the upload button again. \n Thank you!");
    return new JsonResult { Data = new { status = status, message = message } };
}

推荐阅读