score:1

Accepted answer

if i did understand your question, following code should work. as others say, i do think your query is complex, where it does not need to be i think you probably do not want to change what has been working, i just keep it unchanged

var viewfullrecipegrouping =
(
    from data in viewrecipesummary
    group data by data.recipename
    into recipegroup
    let fullingredientgroups = recipegroup.groupby(x => x.ingredientname)
    select new viewfullrecipe()
        {
            recipename = recipegroup.key,
            recipeingredients =
                (
                    from ingredientgroup in fullingredientgroups
                    select
                        new groupingredient
                            {
                                ingredientname = ingredientgroup.key,
                                unitweight = ingredientgroup.average(r => r.unitweight),
                                totalweight = ingredientgroup.sum(r => r.totalweight)
                            }
                ).tolist(),

            viewgrouprecipes =
                (
                    from recipename in
                        viewrecipesummary.groupby(x => x.ingredientname)
                                            .where(g => fullingredientgroups.any(f => f.key == g.key))
                                            .selectmany(g => g.select(i => i.recipename))
                                            .distinct()
                    select new grouprecipe()
                        {
                            recipename = recipename,
                            ingredients =
                                viewrecipesummary.where(v => v.recipename == recipename)
                                                    .groupby(i => i.ingredientname)
                                                    .select(
                                                        g => new groupingredient
                                                            {
                                                                ingredientname = g.key,
                                                                unitweight = g.sum(i => i.unitweight),
                                                                totalweight = g.average(i => i.totalweight)
                                                            }).tolist()
                        }
                ).tolist()
        }).tolist();

score:0

i assume your recipe group defined like this:

public class recipe
{
    public string recipename { get; set; }
    public string ingredientname { get; set; }

    public recipe() { }
    public recipe(string _recipename, string _ingredientname)
    {
        recipename = _recipename;
        ingredientname = _ingredientname;
    }
}

public class recipegroup
{
    public string recipename{get;set;}
    public list<recipe> recipe{get;set;}
}

public class recipegroupdictionary
{
    private dictionary<string, list<recipe>> data;
    public recipegroupdictionary()
    {
        data = new dictionary<string, list<recipe>>();
    }
    public bool add(recipe vr)
    {
        this[vr.recipename].add(vr);
        return true;
    }
    public list<recipe> this[string key]
    {
        get
        {
            if (!data.containskey(key))
                data[key] = new list<recipe>();
            return data[key];
        }
        set
        {
            data[key] = value;
        }
    }
    public icollection<string> keys
    {
        get
        {
            return data.keys;
        }
    }
    public list<recipegroup> getrecipegroup()
    {
        var result = new list<recipegroup>();
        foreach (var key in data.keys)
        {
            result.add(new recipegroup { recipename = key, recipe = data[key] });
        }
        return result;
    }
}

class program
{
    static void main(string[] args)
    {
        var arr = new list<recipe>();
        var recipegroup = new recipegroupdictionary();
        arr.add(new recipe{ recipename="1", ingredientname="a"});
        arr.add(new recipe{ recipename="1", ingredientname="b"});
        arr.add(new recipe{ recipename="1", ingredientname="c"});
        arr.add(new recipe { recipename = "2", ingredientname = "b" });
        arr.add(new recipe { recipename = "2", ingredientname = "c" });
        arr.add(new recipe { recipename = "3", ingredientname = "a" });
        arr.add(new recipe { recipename = "3", ingredientname = "x" });
        arr.add(new recipe { recipename = "3", ingredientname = "y" });

        var rnset = from row in arr
                    where row.ingredientname == "a"
                    select row.recipename;
        var result = (from row in arr
                      where rnset.contains(row.recipename) && recipegroup.add(row) //won't be executed if the first condition is false.
                      select row ).toarray(); //to array is list<recipe>, if you don't want recipe group you can remove all the recipe dictionary related.
        foreach (var key in recipegroup.keys)
        {
            foreach(var recipe in recipegroup[key])
                console.writeline("rn:{0}, ia:{1}", recipe.recipename, recipe.ingredientname);
        }                      
    }
}

Related Query

More Query from same tag