简体   繁体   English

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

[英]LINQ Query - exclude the values of one list from another list

I need some help with a LINQ query please. 我需要一些有关LINQ查询的帮助。

Here's my code : 这是我的代码:

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>();
}

What I need is a LINQ statement that returns the list of sensitivities, but not including the ones that are in the PatientRecord Sensitivities list. 我需要的是LINQ语句,该语句返回敏感度列表,但不包括PatientRecord Sensitivities列表中的那些。

What I'm trying to do is list all sensitivities, in a datagridview control.... the user can then drag and drop one of these onto a treeview control which adds it to the PatientRecord Sensitivities list. 我想做的是在datagridview控件中列出所有敏感度...。然后,用户可以将其中之一拖放到treeview控件中,然后将其添加到PatientRecord Sensitivities列表中。 I'm then wanting the datagridview to refresh with the sensitivites minus the ones already in the Patient Record, so that the same sensitivity can't be added to the treeview twice. 然后,我希望datagridview用敏感度减去“患者记录”中已有的敏感度进行刷新,以使相同的敏感度不能两次添加到树状视图中。

Hope you can help. 希望能对您有所帮助。

假设PatientSensitivitiesListOfSensitivities实际上是一个List而不是某些自定义类型,我认为Except()会满足您的需求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM