c# - 如果字段 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
}
我只是不确定如何在当前代码中放置它。
解决方案
我会在返回结果之前检查所有行。
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 } };
}
推荐阅读
- javascript - 如何在交换机内部使用路由器链接
- javascript - 角色检查器机器人 - Discord.js - ReferenceError:未定义成员
- html - 允许文本换行而不允许其他元素换行?
- java - 基于页面的Birt组头
- vim - 在vim中删除直到第二个正斜杠
- wcf - 禁用 tls1.0 时调用方未通过服务身份验证
- python - 使用Buildozer for android包的MDIcon问题
- kubernetes - 如何将外部指标提供给 HPA?
- python - 抓取网站 JSON 和 Python 上的少量搜索结果
- sdk - Cognos 11 使用 SDK 可以获取 Native SQl(生成的 sql)