question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GroupBy using grouper function that returns complex type doesn not work

See original GitHub issue

When performing a GroupBy on a List<<T>> using a grouper function that returns a “complex” anonymous type, GroupBy returns a flat array of objects (untyped “T” objects) instead of a list of key/value pairs as it does when grouping with a grouper function that returns a primitive type.

e.g with a type.

interface IReferral {
    origin: string;
    modalityType: string;
    qtyReferrals: number;
}

Not working:

let referralsByOrigin = new List<IReferral>(data).GroupBy(key => ({ org: key.origin, modType: key.modalityType}), element => element);

I am currently working around this by creating a primitive returning grouper that returns a concatenation of my grouping elements like below, and then, when processing the result, retrieving the actual grouping elements from the first item. (instead of retrieving them as “menbers” of the grouper result (key) :

let groupedData = new List<IReferral>(result).GroupBy(key => key.origin + '_' + key.modalityType , element => element);

for (var key in groupedData) {
  // count the total nr of referrals
  var totalReferrals = groupedData[key].reduce((a, b) => a + b.qtyReferrals, 0);
  var firstItem : IReferral = new List<IReferral> (groupedData[key]).First();
  this.chartData.push([firstItem.origin, firstItem.modalityType, totalReferrals]);
}

a fix to this would be great!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
marlon-tuckercommented, Oct 15, 2018

This will never work, the object being returned is “new” and therefore always unique. In C# I believe this works due to anonymous objects implementing a simplistic IEquality interface which hashes the properties. This is not achievable in javascript.

You could inspect the mapped return type to see if it contains a “equals” function, and use the return value from that function instead, but in that case, the mapping function is better placed to call it instead.

In short, this is not achievable with the current set of tools javascript provides.

0reactions
kutyelcommented, Oct 25, 2018

I’ll close this for now since as discussed in the thread I don’t think this is possible yet due to the nature of JavaScript itself 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comprehensive Guide to Grouping and Aggregating with ...
Pandas groupby and aggregation provide powerful capabilities for summarizing data. This article will discuss basic functionality as well as ...
Read more >
4 Pandas GroupBy Tricks You Should Know | Medium
Python Pandas Groupby and aggregation functions rename columns, size and count, customising agg functions and cut in bins for data EDA jobs.
Read more >
Group by: split-apply-combine — pandas 1.5.2 documentation
Creating the GroupBy object only verifies that you've passed a valid mapping. Note. Many kinds of complicated data manipulations can be expressed in...
Read more >
python Pandas calling a complex function in groupby.agg
groupby/agg fails because the value returned by the agg function must be a scalar, not a Series.
Read more >
agg on groups with different sizes fails with out of bounds ...
What we probably want when we apply an aggregate function to a ResamplerGroupby subtype is to get data that's grouped by the groupby...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found