Code Red > .NET
Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
nobody:
Want to combine two ObjectIdCollections ... only what to iterate through each objectid and add to the second collection? Suppose I could create lists but just wondering more out of curiosity than anything.
Thanks!
gile:
Hi,
There is no built-in method for combining / concatenating two ObjectIdCollections.
For my part, I avoid using ObjectIdCollection except when the AutoCAD API requires it.
Generic collections (List <Objectid>, HashSet <ObjectId>, ...) are more powerful and more flexible.
You can create an extension method to mimic the List<T>.AddRange() method:
--- Code - C#: --- public static class Extension { public static void AddRange(this ObjectIdCollection ids, ObjectIdCollection otherIds) { foreach (ObjectId id in otherIds) ids.Add(id); } }
By my side, I'd rather use Linq extension methods which return a new IEnumerable<ObjectId> so that the original collections remain unchanged.
--- Code - C#: --- public static class Extension { public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds) { return ids.Cast<ObjectId>().Concat(otherIds); } public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, ObjectIdCollection otherIds) { return ids.Concat(otherIds.Cast<ObjectId>()); } }
Then, if needed, you can easily convert the returned IEnumerable<objectId> into a concrete collection:
--- Code - C#: ---List<ObjectId> ids3 = ids1.Concat(ids2).ToList();
--- Code - C#: ---ObjectIdCollection ids4 = new ObjectIdCollection(ids1.Concat(ids2).ToArray());
nobody:
wow...thank you :) Looks super efficient.
CADbloke:
I think the functionality you are looking for is "Union" - you want to combine the ObjectIdCollections without introducing duplicates.
https://msdn.microsoft.com/en-us/library/bb341731(v=vs.110).aspx
ObjectIdCollection doesn't have it but Linq does so you would need to write it yourself. Perhaps break out the collections into List<>, Union that and then rebuild the ObjectIdCollection
The other way is to convert the ObjectIdCollections into HashTables and then combine those.
gile:
Hi,
If you want to remove duplicates, you can use Union (as CADbloke suggested) instead of Concat in the upper Linq extension methods.
--- Code - C#: --- public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds) => ids.Cast().Union(otherIds); public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, ObjectIdCollection otherIds) => ids.Union(otherIds.Cast());
Navigation
[0] Message Index
[#] Next page
Go to full version