multiple groupby using operator in linq display differently from using expression

ngunha02

I am learning Linq by reading Linq in Action book. On chapter 5 where the author performs multiple grouping. The linq expression is as follow:

var query =
  from book in SampleData.Books
  group new {book.Title, book.Publisher.Name} by book.Subject into grouping
  select new {Subject=grouping.Key.Name, Books=grouping};

The output from author is: enter image description here

I am rewriting by using operator instead of expression. I tried with this but the result showing in LinqPad is not the same as the above:

var query2 = SampleData.Books.GroupBy(
                book => new {book.Subject}
            )
            .Select(book => new {
                    Subject = book.Key.Subject.Name, 
                    Children = book.GroupBy(b => new {b.Subject.Description, b.Subject.Name})}
            );

This is my output: enter image description here

HugoRune

The first query performs a single groupBy, the second performs two nested groupBy.

To accurately represent the first query in method syntax, you need the overload of GroupBy that takes two parameters, one to pick the key for each element, and one to pick the representation of each element within the group

.GroupBy(book => book.Subject, book => new {book.Title, book.Publisher.Name} )

(Note that you do not need to create an anonymous object with only one member: book => book.Subject works just as well as book => new{book.Subject}

Then you can perform the final select over the result of the GroupBy operator:

.Select(grouping => new {Subject=grouping.Key.Name, Books=grouping});

You can also do this all with only the GroupBy command, if you use this even more obscure GroupBy overload that takes three parameters: key-selector, element-selector and result-selector:

.GroupBy(book => book.Subject,  
                 // select a key
         book => new {book.Title, book.Publisher.Name},
                 // select the elements of each group
         grouping => new {Subject=grouping.Key.Name, Books=grouping} ) 
                 // select the final result

This does exactly the same as the previous two commands combined.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Creating hierarchical JSON result from multiple tables using linq expression

来自分类Dev

Exclude a column from a select using LINQ

来自分类Dev

reversing text from input file using linq

来自分类Dev

Linq group by date when using multiple group by fields

来自分类Dev

SELECT encrypted items from a table using LIKE on an expression

来自分类Dev

Using expression from ng-repeat inside an ng-include

来自分类Dev

Get the objects with the maximum value for a specific property from a list using LINQ

来自分类Dev

Using linq with a PropertyInfo

来自分类Dev

Using sum method in LINQ

来自分类Dev

Underscore groupBy using server data

来自分类Dev

Angular Sortable events work differently than using JQuery

来自分类Dev

Inner Join Using Lambda (LINQ)

来自分类Dev

Linq Query using Contains and not contains

来自分类Dev

Copy data from multiple tables using insert into select

来自分类Dev

Select same fields from multiple tables using postgresql

来自分类Dev

Pagination not working using display tag

来自分类Dev

Display VlcJ in JavaFX using SwingNode

来自分类Dev

c# - How to remove matching items from a list using Linq or otherwise

来自分类Dev

Autofac - Using Multiple Implementation

来自分类Dev

PostgreSQL Multiple USING子句

来自分类Dev

Using $cond operator with Spring-data-mongodb

来自分类Dev

GroupBy Expression无法翻译

来自分类Dev

Linq groupby和sum

来自分类Dev

LINQ Groupby查询单列

来自分类Dev

LINQ GroupBy参数化

来自分类Dev

Linq:GroupBy与Distinct

来自分类Dev

Linq .GroupBy()与计数

来自分类Dev

使用GroupBy的Linq查询

来自分类Dev

Linq:GroupBy与Distinct