0

I need some help with a LINQ query please.

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.

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. 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.

Hope you can help.

  • Possible duplicate of [How would you do a "not in" query with LINQ?](http://stackoverflow.com/questions/183791/how-would-you-do-a-not-in-query-with-linq) – Ben Mar 15 '17 at 16:34

1 Answers1

2

Assuming PatientSensitivities and ListOfSensitivities are actually a List and not some custom type, I think an Except() will do what you're looking for.

this.dgvSearchResults.DataSource = ListOfSensitivities.Except(PatientSensitivities).ToArray();
Brendan
  • 4,327
  • 1
  • 23
  • 33