score:2
Accepted answer
If the Reviews
property contains all Review
s for a particular Book
this is simple:
var result = books.Select(book => new
{
book.Title,
Rating = book.Reviews.Average(review => review.Rating)
});
But if you have separate collections you will need to perform a join:
var result = books.GroupJoin(reviews, // One-to-many
book => book.Id, // Primary key
review => review.Book.Id, // Foreign key
(book, reviews) => new // Selection
{
book.Title,
Rating = reviews.Average(review => review.Rating)
});
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- Calculation of the average in linq
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- Get Average Using LINQ
- Calculating Weighted Average with LINQ
- Average extension method in Linq for default value
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Translate SQL to lambda LINQ with GroupBy and Average
- LINQ to calculate a moving average of a SortedList<dateTime,double>
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Linq How to Get Average When all values equals null or 0 ? MVC
- LINQ WHERE method alters source collection
- Using LINQ to Get Sum/ Average of a List with custom objects
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ IEnumerable<T[]> to IEnumerable<T>
- Using linq to instantiate objects containing lists of other objects, where query criteria is on inner list
- Combining two adjacent collections to create Json rows
- How to write a LINQ query to do this
- How to filter entities that are deleted using linq to entities
- How to optimise this LINQ query?
- Using Except on Dictionaries with Object as Value
- LINQ2SQL Any(), NOT EXISTS Problems
- SQL query to linq syntax
- return message in linq
- Add XML after specific node
- How to simplify this LINQ to Entities Query to make a less horrible SQL statement from it? (contains Distinct,GroupBy and Count)
- Multi table join using Entity framework 4.1, should I use lambda or LINQ?
- Partial Search in Linq for space separate values
- Linq to group by two fields and average
- How to search string on list
- getting duplicate rows for each row using linq query
- Is there a way to make this in linq?
- linq query with join
- Unique from list or null in Linq?