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);
}
}
}
Source: stackoverflow.com
Related Articles
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Use a linq query as microsoft local report Data Source (WinForms)
- How to use let to define a new set of data within a LINQ query?
- Using LINQ query result for data source for GridControl c#
- Linq to query data between 2 dates when within a group
- Linq query null check in data source
- Linq Dynamic Query tried to search data within a year get no results
- How to LINQ Query Code First generated EF6 hierarchical entities (entities within entities)?
- c# Linq or code to extract groups from a single list of source data
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to query data within a session variable using linq
- LINQ query code for complex merging of data
- Linq query for Get Data within Date in .net core
- Doing a Cast Within a LINQ Query
- LINQ query — Data aggregation (group adjacent)
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Query data using "Contains" keyword in Dynamic Linq in C#
- Is there a neat way of doing a ToList within a LINQ query using query syntax?
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Retrieving Data from database within the last 7 days using linq
- LINQ - using a query expression to calculate data time difference
- LinQ query with multiple tables and extracting data
- LINQ Query to insert data into the database
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Linq query that reduces a subset of duplicates to a single value within a larger set?
- Linq query - List within another list
- LINQ EF Join query from 2 different data context
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- how to put conditional WHERE clause within LINQ query
- How to use Multi-Mapper to create multiple one to many object hierarchy
- Linq Return List of Objects after max effective From Date
- The specified type member is not supported in LINQ to Entities. - Linq .Contains
- Expression to find all objects that contain all elements in an array of integers
- IQueryable conditional include
- Query dynamic data with LINQ
- Data comparing in dataset
- How to update with LINQ an object property knowing its name?
- How can I create a LINQ expression that joins four tables and allows a where on the topmost table?
- How to create linq predicate with multiple nested "ands" and "ors"
- SL Turn Hierarchical Data into Flat Data to Populate a DataGrid
- are these two linq expressions functionally equivalent?
- Using LINQ to filter XML file
- Unable to cast object of type 'System.Decimal' to type 'System.String'
- C# interpret my query as inner join instead of left outer join on what cases this occurs?
- Convert if-else within a foreach loop to LINQ
- Problem in creating database through code first approach
- LINQ Expression in dataset
- Group by with linq to update collection
- How can i append a Where String in to Linq?