score:1

Accepted answer
enum materialtype
{
    none,
    wood,
    glass,
    steel,
    cloth
}

class ingredient
{

    public ingredient(materialtype type, int amount)
    {
        type = type;
        amount = amount;
    }

    public materialtype type { get; }
    public int amount { get; }
}

class recipe
{

    public recipe(string name, params ingredient[] ingredients) 
        : this(name, (ienumerable<ingredient>) ingredients)
    {          
    }

    public recipe(string name, ienumerable<ingredient> ingredients)
    {
        name = name;
        ingredients = ingredients.toarray();
    }

    public string name { get; }
    public ingredient[] ingredients { get; }
}

[testclass]
public class findallitemsfixture
{

    private readonly static ienumerable<recipe> allitemrecipes = new list<recipe>
    {
        new recipe("sword", new ingredient(materialtype.steel, 3)),
        new recipe("spear", new ingredient(materialtype.steel, 1), new ingredient(materialtype.wood, 3)),
        new recipe("table",  new ingredient(materialtype.wood, 6)),
        new recipe("chair",  new ingredient(materialtype.wood, 4)),
        new recipe("flag",  new ingredient(materialtype.cloth, 2)),

    };

    ienumerable<recipe> getallrecipesusingmaterial(materialtype materialtype)
    {
        return allitemrecipes.where(r => r.ingredients.any(i => i.type == materialtype));
    }


    [testmethod]
    public void getallwoodenrecipes()
    {
        var expectednames = new string[] { "spear", "table", "chair" };
        var woodenitems = getallrecipesusingmaterial(materialtype.wood);
        collectionassert.areequal(expectednames, woodenitems.select(i => i.name).toarray());

    }

    [testmethod]
    public void getallclothrecipes()
    {
        var expectednames = new string[] { "flag" };
        var clothitems = getallrecipesusingmaterial(materialtype.cloth);
        collectionassert.areequal(expectednames, clothitems.select(i => i.name).toarray());

    }
}

score:1

var woodrecipes = recipes.where(r => r.requireditems.contains(wood)).tolist();

Related Query

More Query from same tag