how can I filter dups from this list? [duplicate]

Multi tool use
Multi tool use


how can I filter dups from this list? [duplicate]



This question already has an answer here:



I have the following list of objects:


{
DomainName="my-corp-1",
GroupName="group-x",
Desription="my group x"
}

{
DomainName="my-corp-2",
GroupName="group-x",
Desription="my group x - follow up with admin"
}

{
DomainName = "my-corp-1",
GroupName="group-y",
Description="my group y"
}



If an object with the same GroupName exists in both domains then I should keep the object which exists in my-corp-1 and filter out the object with the same GroupName associated with my-corp-2.



For example, in the list of objects above, the second object in the list should be filtered out b/c the GroupName already exists for group-x in the first object.



What would be a clean way to implement this filtering routine in C#?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





What have you tried to do that is not working?
– MineR
Jul 3 at 1:00





Look at Linq Distinct
– MineR
Jul 3 at 1:05




1 Answer
1



What's clean tends to be a personal preference :)



I would probably group them by the GroupName, and then act from there. Keeping data until the absolute last step is usually preferable.


// Get an IEnumerable of IGrouping with GroupName as key
var objectsGroupedByGroupName = myDomainList.GroupBy(d => d.GroupName);

// Recklessly throw away data and select only the first object in each grouping per GroupName
var listOfDomainsWithUniqueGroupNames = objectsGroupedByGroupName.Select(x => x.First());



If you just want it more clean, you can of course combine them:


var myUniqueDomainsFromGroupName = myDomainList
.GroupBy(d => d.GroupName) // Group domains by their group name
.Select(x => x.First()) // Select the first entry for each group
//.ToList() if you want a list back
;

M6fSOWfluiQumZQWOtkRU Byu9HcYbAluIWVtmmm
mdu,swaccyw

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications