Urgent.News

What's breaking now, across thousands of outlets.

Tech

LINQ GroupBy: The Operator Everyone Uses Wrong

LINQ GroupBy: The Operator Everyone Uses Wrong GroupBy in LINQ looks like SQL GROUP BY . It isn't. At least, not in the way you'd expect. When you grasp the difference, you stop fighting the operator and start wielding it. The SQL Mental Model (That Misleads You) In SQL, GROUP BY collapses rows into aggregates: SELECT CategoryId , COUNT ( * ) as Count , AVG ( Price ) as AvgPrice FROM Products…

LINQ's GroupBy operator often confuses developers who expect it to behave like SQL's GROUP BY. In SQL, GROUP BY collapses rows into aggregates, producing one row per group with aggregate values. However, LINQ's GroupBy returns a list of IGrouping<TKey,TElement> objects, each containing a key and all the elements in that group. This difference can lead to confusion for SQL-trained developers.

To achieve SQL-like results with LINQ's GroupBy, combine it with Select to perform aggregation. This pattern translates cleanly to SQL when computing computed values for each group. The split between the database and in-memory objects can cause issues when trying to materialize full objects within groups, as some EF Core versions struggle with this pattern.

GroupBy can take composite keys by using anonymous types for the grouping key, allowing items with matching properties to be placed in the same group. Additionally, you can transform elements during grouping by applying an element selector function. This allows you to group elements based on a property while only keeping a specific subset of their properties, such as grouping products by category and keeping only their names.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

From Zero to 3 Merged PRs: Contributing Python CLI Recipes to BasedHardware/omi

Over the past few weeks, I’ve been contributing to BasedHardware/omi , the open-source AI wearable project. As of this week, all three of my pull requests have been officially approved and merged into…

  • Three Python CLI Recipe pull requests merged into main branch
  • Recipes converted data to Markdown, memory stream, and task lists
  • Author learned to rely on Python's standard library and welcome feedback

We Chose the Actions by What Was Easy to Build

A manager came back from three weeks' leave in February and found that a colleague had been reading her mailbox. Nobody had told her before, during or after.

  • A colleague accessed a manager's mailbox in four minutes via a self-service tool.
  • The tool, built in 2022, logged 1,900 actions, 310 of which were mailbox delegations.
  • Of the 310 mailbox delegation actions, three were moved behind an approval process.

More from Wednesday 23 September →