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 Query
- How to use let to define a new set of data within a LINQ query?
- Use a linq query as microsoft local report Data Source (WinForms)
- C# How to declare new Dictionary object based off a string array within a LINQ query
- How to use the vb equivalent of ++ for an index property in a Linq query projecting a new type
- How to use linq to calculate average from grouped data where value > 0 using let
- How do I use LINQ to return the first item from each set of Grouped data (In VB.Net)
- How to set Object instance value equal null if there is no data found in Left Join in LINQ Query
- How to LINQ Query Code First generated EF6 hierarchical entities (entities within entities)?
- How to ensure right set of data in objects returned from LINQ to DataSet Query
- How to use Linq Aggregate Query to print line sequence number alongwith some data from the List<string>?
- How to use linq query on list to find last data by name and number c#?
- 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 assign LINQ Query to a variable and then use it later in the code
- How to Use Enumeration within Linq Query to Update Multiple Rows in a Database
- How to query data within a session variable using linq
- Use multiple left joins to set DTO property inside select new linq query
- How to use Linq Query in Object Data Source?
- How to use index/position with Where in LINQ query language?
- How to use LINQ to order within groups
- How can I set properties on all items from a linq query with values from another object that is also pulled from a query?
- How to I use TryParse in a linq query of xml data?
- How to use LINQ to find all combinations of n items from a set of numbers?
- LINQ How to define a default type for use with ElementAtOrDefault Operator
- How to use LINQ to generate new type of objects
- How to use Union in Linq Query
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How do I report progress while executing a LINQ expression on a large-ish data set
- How does linq actually execute the code to retrieve data from the data source?
- How can I switch that code to use LINQ
- c#: how to set combobox valuemember from linq query
More Query from same tag
- Multiple enumeration and use of Any()
- How to select a column after GroupBy in LINQ?
- How to filter collection in linq based on some subobjects in that list
- How to GroupBy based on condition
- using <T>(T obj) with a Linq query
- EF core Linq groupby and having sum count - could not be translated and will be evaluated locally
- Dynamic list of button with events from xml
- How to flatten class containing an IEnumerable of a related class
- Clean way to reduce many TimeSpans into fewer, average TimeSpans?
- Flatten data in linq
- Generic List Performance Optimization
- Combine/Merge 3 lists of objects based on condition
- Querying data using LINQ in C#
- How to make the Linq2Sql understand custom types?
- Generate Linq dynamically but throw exception when string[] is null
- Child List Element Gets Only 1 Record in Entity Framework
- How to get max element in a string array by linq?
- Remove items from List<string> that exist in Substring of CheckedListBox items
- Need to join two strings if both contain values, or return one value if first is NULL
- Syntax using LINQ ToList to cast GENERIC list to list of its base type
- Creating new string lists from a Dictionary of lists ( i.e from a dictionary which has string Lists as values)
- reflection on an extension method
- using Linq To Sql to save to an SDF (local database file) in .Net
- Filtering a datasource on Click Event
- Read text file word-by-word using LINQ
- Create hyperlinks in MVC Action Method
- Implement search in collection with transliteration
- c# code to read xml file values
- Replacing CompareTo with LINQ for array elements
- What exactly is a circular reference?