GroupBy using grouper function that returns complex type doesn not work
See original GitHub issueWhen 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:
- Created 6 years ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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 🤷♂️