c# - 列出对象检查两个属性
问题描述
在我的代码示例中,有四个患者父亲、儿子、孙子和母亲,但只选择了一个。在查找表中,父亲与儿子相关联,儿子与孙子相关联。现在它需要同时检查 Patientid 和 childid,因为 Grandson 处于 childid 中,它将返回 isvalid false。所有三个父亲,儿子和孙子都需要被选中。同样因为母亲不在查找表中,它也应该返回 Mother isvalid true。我被困在我 selectedLookup 表的位置我需要再次循环检查 selectedLookup 吗?这是返回 true 但应该返回 false 因为没有选择父亲和儿子
用例 1 选择了母亲,因为查找表中没有母亲的值,所以应该返回 IsValid true 这是有效的
用例 2 父亲、儿子、孙子所有关系都被选中 这也应该返回 return IsValid true 这正在工作
用例 3 父亲,儿子被选中这是缺少孙子,所以它应该返回 IsValid false 这不起作用它返回 true
用例 4 孙子只是被选中的一个,但因为它与儿子有关,所以它应该返回 false 这不起作用它返回 true
class Program
{
static void Main(string[] args)
{
Boolean IsValid = true;
List<Patient> Patients = new List<Patient>();
Patients.Add(new Patient() { id = 1, Name = "father", IsSelected = false });
Patients.Add(new Patient() { id = 2, Name = "Son", IsSelected = false });
Patients.Add(new Patient() { id = 3, Name = "Grandson", IsSelected =true });
Patients.Add(new Patient() { id = 4, Name = "Mother", IsSelected = false });
List<Lookup> Lookup = new List<Lookup>();
Lookup.Add(new Lookup() { id = 1, patientid=1, childid=2 });
Lookup.Add(new Lookup() { id = 2, patientid = 2, childid = 3 });
var selectedPatients= Patients.Where(x => x.IsSelected).ToList();
foreach (var Patient in selectedPatientsSelected)
{
var selectedLookup = Lookup.Where(y => y.childid == Patient.id || y.patientid == Patient.id).ToList();
foreach (var relation in selectedLookup)
{
var related = relation;
if (selectedLookup.All(t => t.patientid != related.id))
{
IsValid = false;
Console.WriteLine("Process relation {0}", IsValid);
}
}
}
Console.WriteLine("Process relation {0}", IsValid);
Console.Read();
}
}
class Patient
{
public int id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
class Lookup
{
public int id { get; set; }
public int patientid { get; set; }
public int childid { get; set; }
}
解决方案
基本上你所说的是你的每一对都Patient
应该Lookup
具有相同的IsSelected
价值。这我们可以很容易地检查。我将patients
列表转换为 aDictionary<int, Patient>
以便我们可以Patient
通过他/她有效地查找 a Id
。之后我们要做的就是检查所有lookup
条目是否满足条件:
var patients = new List<Patient> {
new Patient { id = 1, Name = "father", IsSelected = false },
new Patient { id = 2, Name = "Son", IsSelected = true },
new Patient { id = 3, Name = "Grandson", IsSelected = false },
new Patient { id = 4, Name = "Mother", IsSelected = true }
};
var lookup = new List<Lookup>
{
new Lookup { id = 1, patientid=1, childid=2 },
new Lookup { id = 2, patientid = 2, childid = 3 }
};
var patientDictionary = patients.ToDictionary(p => p.id);
var IsValid = lookup.All(l => patientDictionary[l.patientid].IsSelected == patientDictionary[l.childid].IsSelected);
我们完成了。请记住,虽然您可能错过了一些边界案例(例如,如果没有选择患者,它可能无效),但这满足您提到的所有案例。
希望这可以帮助!
编辑:
显然,您实际上并没有具有IsSelected
属性的患者列表,但您有一个包含所有选定患者的列表。这也可以使用,但是Dictionary
我们可以只使用HashSet
包含所有选定患者的 id 的而不是使用 a:
var patients = new List<Patient> {
new Patient { id = 1, Name = "father", IsSelected = false },
new Patient { id = 2, Name = "Son", IsSelected = false },
new Patient { id = 3, Name = "Grandson", IsSelected = false },
new Patient { id = 4, Name = "Mother", IsSelected = true }
};
var lookup = new List<Lookup>
{
new Lookup { id = 1, patientid=1, childid=2 },
new Lookup { id = 2, patientid = 2, childid = 3 }
};
var selectedPatients = patients.Where(x => x.IsSelected).ToList();
var selectedPatientsHashSet = selectedPatients.Select(p => p.id).ToHashSet();
var IsValid = lookup.All(l => selectedPatientsHashSet.Contains(l.patientid) == selectedPatientsHashSet.Contains(l.childid));
对于我们拥有的每个关系,它们要么都需要包含在HashSet
(因此它们都被选中)中,要么它们都不应该包含在HashSet
(未选中)中。
推荐阅读
- android - 滚动片段中具有灵活内容的Android布局问题
- google-analytics - 防止 gtag / 全局网站代码跟踪初始页面加载
- android - 如何使用 Hilt 在 Room TypeConvertors 中注入 Moshi/Gson?
- r - 使用 pre() ,Rulefit 包预测函数使用情况
- animation - SVG动画丢弃元素
- javascript - 世博会推送通知令牌如何工作?指数推送令牌[XYC]
- python - 如何将占位符添加到 Django 生成的输入中?
- android - 运动场景错误。无法使用运动编辑器
- java - 我需要从 fireabse 调用数据
- windows - 仅对真正的可执行文件进行数字签名,而不是覆盖/附加数据