It cannot be done directly in LINQ, it requires some additional code from your side.
If your objects (In this case RRProcess
.) doesn't have any members that are objects, then you can implement a clone method for your object like below:
public RRProcess Clone()
{
return this.MemberwiseClone();
}
I shall note that MemberwiseClone
will only produce a shallow clone, hence why it will not clone objects.
In cases where RRProcess
implements objects and you need to clone those too then you have to perform a deep clone.
See this answer: https://stackoverflow.com/a/129395/2026276
You can use the above to implement ICloneable
see: https://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110).aspx
However I advice you in your case to implement it without, because Clone()
from ICloneable
only returns an object which will require further casting from your side and it may not be worth it in your case.
What you can then do is this:
List<RRProcess> noDuplicates = pSorted
.GroupBy(i => i.pID)
.Select(group => group.First().Clone())
.ToList();
If you implement ICloneable
List<RRProcess> noDuplicates = pSorted
.GroupBy(i => i.pID)
.Select(group => (RRProcess)group.First().Clone())
.ToList();