C
C#2y ago
SWEETPONY

✅ how to remove duplicates?

I have the following method for preparing a dataset. The problem is that I receive territory ids and use them to group and form the dataset. For some reason, if multiple ids are passed at once and the data has something in common, it gets duplicated: accessGroup.Id, accessGroup.Title, accessGroup.Description. Please help me understand where the mistake is. group:
var groupedAccessGroups = accessGroups
.SelectMany(accessGroup => accessGroup.Compositions,
(accessGroup, compositions) => (
accessGroup.Id,
accessGroup.Title,
accessGroup.Description,
Compositions: compositions
))
.Where( accessGroup => territoriesIDs.Contains( accessGroup.Compositions.TerritoryId ) )
.GroupBy( accessGroup => accessGroup.Compositions.TerritoryId );
var groupedAccessGroups = accessGroups
.SelectMany(accessGroup => accessGroup.Compositions,
(accessGroup, compositions) => (
accessGroup.Id,
accessGroup.Title,
accessGroup.Description,
Compositions: compositions
))
.Where( accessGroup => territoriesIDs.Contains( accessGroup.Compositions.TerritoryId ) )
.GroupBy( accessGroup => accessGroup.Compositions.TerritoryId );
4 Replies
phaseshift
phaseshift2y ago
SelectMany doesn't guarantee uniqueness...
SWEETPONY
SWEETPONYOP2y ago
soo what to do? hm I can do smth like this .DistinctBy( x => new { x.Compositions.TerritoryId, x.Id, x.Title, x.Description } ) is it a right solution?
phaseshift
phaseshift2y ago
idk, does it work?
SWEETPONY
SWEETPONYOP2y ago
yes
Want results from more Discord servers?
Add your server