Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework groupby children collection

I use Entity Framework 6, I have such database structure (simplified):

Transactions - Id - DateTime - ...

Products - Id - Name

TransactionsItems - Id - TransactionId - ProductId

As you see relation Transaction <-> Product is many to many type.

I don't know how to group transactions by products using LINq - something like

transactions.GroupBy(t => t.TransactionsItems.Product)

where TransactionsItems property is navigation property generated by entity framework and contains collection of items related to this transaction.

I know I can iterate all products and then search for transactionItems and accumulate data, but I wonder whether is there any simplier method to achieve my goal?

like image 743
Lukas Avatar asked Dec 21 '25 01:12

Lukas


1 Answers

Maybe you just got to group by a specific column :

transactions.GroupBy(t => t.TransactionsItems.Product.Name)
like image 90
Antoine Pelletier Avatar answered Dec 22 '25 17:12

Antoine Pelletier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!