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 Articles