LINQ查询 - 从另一个列表中排除一个列表的值

时间:2017-03-15 16:31:48

标签: c# linq

我需要一些LINQ查询的帮助。

这是我的代码:

private void ReturnSensitivitiesSearchResults()
{

    PatientRecord PMR = new PatientRecord();        

    // Return the list of sensitivities (Ingredient objects), but do not include any where the "SuitableForSensitivityChecking" is false - as we wouldn't want to include ingredients in the list where
    // they can't be screened for sensitivities. - This returns an array of Ingredient[]
    var ListOfSensitivities = FDBSystem.Navigation.GetIngredientsByName(txtSearchText.Text + "*", sensitivityType).Where(n => n.SuitableForSensitivityChecking != false);

    // Return a list of all of the Sensitivities logged on a PatientRecord (which is a list of Ingredient objects)
    var PatientSensitivities = PMR.Sensitivities;

    // Populate the drug information into the grid control.

    this.dgvSearchResults.DataSource = ListOfSensitivities.ToArray();
}


class PatientRecord
{
    public List<Ingredient> Sensitivities = new List<Ingredient>();
}

我需要的是一个LINQ语句,它返回敏感列表,但不包括PatientRecord Sensitivities列表中的敏感列表。

我尝试做的是列出所有敏感度,在datagridview控件中....用户可以将其中一个拖放到树视图控件上,然后将其添加到PatientRecord Sensitivities列表中。我希望datagridview能够使用sensitivites减去患者记录中已有的敏感度,以便不能将相同的敏感度添加到树视图中两次。

希望你能提供帮助。

1 个答案:

答案 0 :(得分:2)

假设PatientSensitivitiesListOfSensitivities实际上是List而不是某种自定义类型,我认为Except()会做你正在寻找的事情。

this.dgvSearchResults.DataSource = ListOfSensitivities.Except(PatientSensitivities).ToArray();