首页 > 解决方案 > 列出对象检查两个属性

问题描述

在我的代码示例中,有四个患者父亲、儿子、孙子和母亲,但只选择了一个。在查找表中,父亲与儿子相关联,儿子与孙子相关联。现在它需要同时检查 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; }
}

标签: c#list

解决方案


基本上你所说的是你的每一对都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(未选中)中。


推荐阅读