c# - How to use let to define a new set of data within a LINQ query? -


i have class represents ingredients recipe

public class viewrecipe {     public string recipename { get; set; }     public string ingredientname { get; set; }     public double unitweight { get; set; }     public double totalweight { get; set; }      public viewrecipe() { }     public viewrecipe(string _recipename, string _ingredientname, double _unitweight, double _totalweight)     {         recipename = _recipename;         ingredientname = _ingredientname;         unitweight = _unitweight;         totalweight = _totalweight;     } }(in reality there lot more data members quantity, weight, etc....) 

the actual dataset list called viewrecipesummary , looks this:

recipename ingredientname 1          1          b 1          c 2          d 2          e 3          3          z 

i have existing query finds ingrident (recipegroup.key) , need following:

1- find recipes have ingredient

2- return rows data recipes

and need use inside query (so needs let or something)

lets assume looking recipes share ingredient a, expect end result of let (based on data show above) this:

recipename ingredientname 1          1          b 1          c 3          3          z 

so recipe has ingredient (1 , 3) return rows of recipes have ingredients need. , should in same form viewrecipe class (not new {recipename, list}, etc...), should provide same rows of data in same format.

current linq query:

            viewfullrecipegrouping = (                 data in viewrecipesummary                 group data data.recipename recipegroup                 let fullingredientgroups = recipegroup.groupby(x => x.ingredientname)                 select new viewfullrecipe()                 {                     recipename = recipegroup.key,                     recipeingredients = (                         ingredientgroup in fullingredientgroups                         select new groupingredient()                         {                             ingredientname = ingredientgroup.key,                         }                     ).tolist(),                     viewgrouprecipes = (              // need add code can use result below             let = .....             // have no clue how perform such query here                          select new grouprecipe()                         {             // use results found generate recipe group             // based on results found above             recipename = a.key                          }).tolist(),                 }).tolist(); 

something like:

                    let = viewrecipesummary.groupby(x => x.recipename)                         .where(g => g.any(x => x.ingredientname == recipegroup.key))                         .select(g => new viewrecipe()                             {                                 recipename = g.key,                                 ingredientname = g.select(x => x.ingredientname)                             })  

but returns list , need return broken down same structure.

here classes used:

    public class grouprecipe     {         public string recipename { get; set; }         public list<groupingredient> ingredients { get; set; }          public grouprecipe() { }         public grouprecipe(string _recipename, list<groupingredient> _ingredients)         {             recipename = _recipename;             ingredients = _ingredients;         }     }     public class groupingredient     {         public string ingredientname { get; set; }         public double unitweight { get; set; }         public double totalweight { get; set; }          public groupingredient() { }         public groupingredient(string _ingredientname, double _unitweight, double _totalweight)         {             ingredientname = _ingredientname;             unitweight = _unitweight;             totalweight = _totalweight;         }     }     public class viewfullrecipe     {         public string recipename { get; set; }         public list<groupingredient> recipeingredients { get; set; }         public list<grouprecipe> viewgrouprecipes { get; set; }          public viewfullrecipe() { }         public viewfullrecipe(string _recipename, list<groupingredient> _recipeingredients, list<grouprecipe> _viewgrouprecipes)         {             recipename = _recipename;             recipeingredients = _recipeingredients;             viewgrouprecipes = _viewgrouprecipes;         }     } 

if did understand question, following code should work. others say, think query complex, not need think not want change has been working, keep unchanged

var viewfullrecipegrouping = (     data in viewrecipesummary     group data data.recipename     recipegroup     let fullingredientgroups = recipegroup.groupby(x => x.ingredientname)     select new viewfullrecipe()         {             recipename = recipegroup.key,             recipeingredients =                 (                     ingredientgroup in fullingredientgroups                     select                         new groupingredient                             {                                 ingredientname = ingredientgroup.key,                                 unitweight = ingredientgroup.average(r => r.unitweight),                                 totalweight = ingredientgroup.sum(r => r.totalweight)                             }                 ).tolist(),              viewgrouprecipes =                 (                     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(); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -