Author Topic: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?  (Read 89 times)

0 Members and 1 Guest are viewing this topic.

FriendFromArea51

  • Swamp Rat
  • Posts: 579
  • Mars sucks!
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!
- Area51Visitor

gile

  • Water Moccasin
  • Posts: 2080
  • Marseille, France
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#: [Select]
  1.    public static class Extension
  2.    {
  3.        public static void AddRange(this ObjectIdCollection ids, ObjectIdCollection otherIds)
  4.        {
  5.            foreach (ObjectId id in otherIds) ids.Add(id);
  6.        }
  7.    }

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#: [Select]
  1.    public static class Extension
  2.    {
  3.        public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds)
  4.        {
  5.            return ids.Cast<ObjectId>().Concat(otherIds);
  6.        }
  7.  
  8.        public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, ObjectIdCollection otherIds)
  9.        {
  10.            return ids.Concat(otherIds.Cast<ObjectId>());
  11.        }
  12.    }

Then, if needed, you can easily convert the returned IEnumerable<objectId> into a concrete collection:

Code - C#: [Select]
  1. List<ObjectId> ids3 = ids1.Concat(ids2).ToList();

Code - C#: [Select]
  1. ObjectIdCollection ids4 = new ObjectIdCollection(ids1.Concat(ids2).ToArray());
Speaking English as a French Frog

FriendFromArea51

  • Swamp Rat
  • Posts: 579
  • Mars sucks!
wow...thank you :) Looks super efficient.
- Area51Visitor

CADbloke

  • Bull Frog
  • Posts: 277
  • Crash Test Dummy
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

  • Water Moccasin
  • Posts: 2080
  • Marseille, France
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#: [Select]
  1.        public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds) =>
  2.            ids.Cast().Union(otherIds);
  3.  
  4.        public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, ObjectIdCollection otherIds) =>
  5.            ids.Union(otherIds.Cast());
« Last Edit: August 18, 2017, 02:54:41 am by gile »
Speaking English as a French Frog

FriendFromArea51

  • Swamp Rat
  • Posts: 579
  • Mars sucks!
Thanks again :)
- Area51Visitor