-1

I have two lists which are different

public Ingrédient(int p_noIngrédient, string p_nomIngrédient, bool p_périssable,
                 double p_prixAuKilo)
{
    NoIngrédient = p_noIngrédient;
    NomIngrédient = p_nomIngrédient;
    Périssable = p_périssable;
    PrixAuKilo = p_prixAuKilo;
}

public Recette(int p_noPlat, int p_noIngrédient, double p_quantité)
{
    NoPlat = p_noPlat;
    NoIngrédient = p_noIngrédient;
    Quantité = p_quantité;
}

I want to find all NoIngrédientin Ingrédient that are not in Recette. Right now I have this but it doesn't work.

void RetraitIngrédient(List<Recette> p_recettes,ref List<Ingrédient> p_ingrédients)
{
    foreach (Recette recettes in p_recettes)
    {
        Ingrédient ingrédients =
            p_ingrédients.Find(i => i.NoIngrédient != recettes.NoIngrédient);

        WriteLine("{0,6} : {1:6}",ingrédients.NoIngrédient, ingrédients.NomIngrédient);
    }
}
  • 1
    [http://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list](http://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list) – Jared Stroebele Dec 15 '16 at 22:34
  • I thought with the WriteLine that it would write all the items that are not present in the other list but it's blank. – Gabriel Lemire Dec 15 '16 at 22:36
  • @JaredStroeb I tried this method but because of the ref it is not working. – Gabriel Lemire Dec 15 '16 at 22:37
  • why you need ref on the list, for what you ar egoing to use the reference ? What is WriteLine ? Why properties, methods in not english language ? – mybirthname Dec 15 '16 at 22:38
  • @mybirthname the ref is because the method is used to remove the element from the `Ingrédient` list after the user selects which of the element he wants to delete, `WriteLine` is to show the user which element he can delete, it is in french because it's a school project and i'm from Quebec. – Gabriel Lemire Dec 15 '16 at 22:42
  • I tried this `void RetraitIngrédient(List p_recettes,ref List p_ingrédients) { var ingrédientNonUtilisé= p_recettes.Where(p => !p_ingrédients.All(p2 => p2.NoIngrédient == p.NoIngrédient)); }` but I have an error at !p_ingrédients because of the ref. – Gabriel Lemire Dec 15 '16 at 22:57
  • 1
    It is a hack but I think you can do something like 'List temp = p_ingrédients' do the linq any function from the question I referenced remove what is needed and reassign` p_ingrédients = temp` to send any changes back. – Jared Stroebele Dec 15 '16 at 22:57
  • It is working! thank you very much! – Gabriel Lemire Dec 15 '16 at 23:11
  • Just curious, have you tried declaring the types in the variables you are mapping to in your public methods? (public Ingrédient(...) and public Recette(...)), (i.e., var NoIngrédient = p_noIngrédient; ). Also, your public methods are not returning any thing. – nocturns2 Dec 15 '16 at 23:11
  • Also, since, you're passing them as lists of types, I'm wondering if you are intending them to be structs or classes. – nocturns2 Dec 15 '16 at 23:19
  • What do I miss? `var NoIngrédient = Recette.Except(Ingrédient);` – L.B Dec 15 '16 at 23:20

1 Answers1

0

you could try something like this.

        var results = _listIngredient.Join(_listRecette,
                        i => i.NoIngrédient,
                        r => r.NoIngrédient,
                        (ingre, rece) => new { ingre.NoIngrédient, ingre.NomIngrédient, ingre.PrixAuKilo, ingre.Périssable });
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51