I am trying to bulk insert using EF (model first) on two tables that have a FK relationship (one to many). The code below properly inserts all of the Challenge entries but only inserts the X amount Shortages one time. My expectation... I have 10 shortages with 2 challenges. I should receive 2 challenge entries and 20 shortage entries. I am only seeing 10 challenge entries for the first shortage inserted. (the code below is simplified)
//class for cloning Shortage collection
public class ShortageCollection : Collection<Shortage>
{
public ShortageCollection(IList<Shortage> source) : base(source) { }
public ShortageCollection() { }
public ShortageCollection Clone()
{
return Clone(this);
}
public static ShortageCollection Clone(ShortageCollection shortage)
{
var res = new ShortageCollection();
foreach (var s in shortage)
{
res.Add(s.Clone());
}
}
}
public class Shortage : StandardDB.Shortage
{
public Shortage Clone()
{
return new Shortage()
{
PART_NUMBER = this.PART_NUMBER,
Note = this.Note,
Qty = this.Qty,
ResponseMachine = this.ResponseMachine
};
}
}
public void CreateChallenge()
{
var JSONJobs = new JavaScriptSerializer().Deserialize<string[]>(Jobs);
var JSONParts = new JavaScriptSerializer().Deserialize<ChallengePartsList[]>(Parts);
using (ARTEntities art = new ARTEntities())
{
art.Configuration.AutoDetectChangesEnabled = false;
art.Configuration.ValidateOnSaveEnabled = false;
ShortageCollection sColl = new ShortageCollection();
foreach(var part in JSONParts)
{
Shortage s = new Shortage()
{
PART_NUMBER = part.Invid,
Note = Challenge,
Qty = part.Qty,
ResponseMachine = ResponseMachine
};
sColl.Add(s);
}
foreach (var job in JSONJobs) {
Challenge c = new Challenge()
{
InitiatorORG = Org,
TypeID = TypeID,
DISCRETE_JOB = job,
InitiatorPERSON_ID = InitiatorPersonID,
InitiatedDate = datenow,
Challenge1 = Challenge,
ChampionGroupID = ChampionGroupID,
StatusID = StatusID,
InitiatorGroupID = InitiatorGroupID,
DivisionID = DivisionID,
Shortages = sColl.Clone()
};
art.Challenges.Add(c);
}
art.SaveChanges();
}
}
you are only creating 10 Shortages in memory. Your Clone method is a shallow clone, and doesn't go through and clone every object. Therefore you have 2 lists with 10 identical items (each pair of items point to exactly same memory reference).
What you need to do is DeepClone which is something like the following:
public static ShortageCollection Clone(ShortageCollection shortage)
{
var res = new ShortageCollection();
foreach(var s in shortage) {
res.Add( s.Clone() );
}
return res;
}
And in your shortage class:
public class Shortage
{
public Shortage Clone()
{
return new Shortage()
{
SomeProp = this.SomeProp,
SomeOtherProp = this.SomeOtherProp
}
}
}
Be aware that if inside shortage any of those objects point to another entity, each pair will point to the same entity.
Search for DeepClone for more info
Related
I built a class Cluster as follow:
public class Cluster
{
List<Cluster> lstChildClusters=new List<Cluster>();
public List<Cluster> LstChildClusters
{
get { return lstChildClusters; }
set { lstChildClusters = value; }
}
public classA alr;
}
My goal is to build a function that gets all the grandchildren of an object of Cluster type.Basically a father can have 0 or more sons which can have at their turn 0 or more sons.
I tried to build a recursive function but all it gives back is only one grandchild using the code down below.
Here is the function I built:
public List<classA> getLevel0Clusters(Cluster cluster,List<classA> list)
{
if (cluster.LstChildClusters.Count == 0)
{
list.Add(cluster.alr);
return (list);
}
else
{
for (int i = 0; i < lstChildClusters.Count - 1; i++)
{
return (lstChildClusters[i].getLevel0Clusters(lstChildClusters[i], list));
}
return (lstChildClusters[0].getLevel0Clusters(lstChildClusters[0], list));
}
}
I am using those instances for debugging:
Cluster father = new Cluster();
father.Alr = new Alarm("father");
Cluster son1 = new Cluster();
son1.Alr = new Alarm("son1");
Cluster son2 = new Cluster();
son2.Alr = new Alarm("son2");
Cluster grandson1 = new Cluster();
grandson1.Alr = new Alarm("grandson1");
Cluster grandson2 = new Cluster();
grandson2.Alr = new Alarm("grandson2");
father.LstChildClusters.Add(son1);
father.LstChildClusters.Add(son2);
son1.LstChildClusters.Add(grandson1);
son1.LstChildClusters.Add(grandson2);
List<classA> lst=new lst<ClassA>();
lst=father.getLevel0Clusters(father, father.LstAlarms);
Does anybody has any clue on how to troubleshoot this problem?
Thank you in advance
There are a number of problems with your existing code, so I've done a bit of refactoring to make your program simpler.
But first, to answer your direct question, the problem with your existing method is that you're calling return before you finish aggregating all of the results. Your code looks at grandfather and sees that it has children so it enters the for loop and recursively calls itself for son1. It sees that son1 has children so enters the for loop and recursively calls itself for grandson1 which doesn't have children so it adds grandson1 to the list and then returns. The outer call returns after finding the first value so the next two levels up just return. Hence the list only has grandson1.
So, to refactor your code: The getLevel0Clusters method does not need to pass in a Cluster (as it is defined in the Cluster class it can use this) and a List<classA> (as it can generate one as needed).
So your getLevel0Clusters can become simply this:
public List<classA> getLevel0Clusters()
{
return new[] { this.alr, }
.Concat(this.LstChildClusters
.SelectMany(child => child.getLevel0Clusters()))
.ToList();
}
In order to get everything to compile I modified your sample code to be this:
Cluster father = new Cluster();
father.alr = new classA("father");
Cluster son1 = new Cluster();
son1.alr = new classA("son1");
Cluster son2 = new Cluster();
son2.alr = new classA("son2");
Cluster grandson1 = new Cluster();
grandson1.alr = new classA("grandson1");
Cluster grandson2 = new Cluster();
grandson2.alr = new classA("grandson2");
father.LstChildClusters.Add(son1);
father.LstChildClusters.Add(son2);
son1.LstChildClusters.Add(grandson1);
son1.LstChildClusters.Add(grandson2);
List<classA> lst = father.getLevel0Clusters();
...and your classes as this:
public class Cluster
{
List<Cluster> lstChildClusters = new List<Cluster>();
public List<Cluster> LstChildClusters
{
get { return lstChildClusters; }
set { lstChildClusters = value; }
}
public classA alr;
public List<classA> getLevel0Clusters()
{
return new[] { this.alr, }
.Concat(this.LstChildClusters
.SelectMany(child => child.getLevel0Clusters()))
.ToList();
}
}
public class classA
{
public string Name;
public classA(string name)
{
this.Name = name;
}
}
When I ran your sample code I got this result out:
The problem is that as soon as you find one offspring, you return to the calling program. The value of Count has no effect other than 0 versus positive: you enter the loop, call lstChildClusters[0].getLevel0Clusters(lstChildClusters[0], and return that value without bothering to increment i and continue the loop.
Instead, your for loop has to add each return value to the list. After the loop is done, you can return to the calling program.
I have a really simple program that creates a bunch of objects and iterates through them to set each object's Priority property.
static void Main(string[] args)
{
foreach (var obj in ObjectCreator.CreateObjectsWithPriorities())
Console.WriteLine(String.Format("Object #{0} has priority {1}",
obj.Id, obj.Priority));
}
class ObjectCreator
{
public static IEnumerable<ObjectWithPriority> CreateObjectsWithPriorities()
{
var objs = new[] { 1, 2, 3 }.Select(i => new ObjectWithPriority() { Id = i });
ApplyPriorities(objs);
return objs;
}
static void ApplyPriorities(IEnumerable<ObjectWithPriority> objs)
{
foreach (var obj in objs)
{
obj.Priority = obj.Id * 10;
Console.WriteLine(String.Format("Set priority of object #{0} to {1}", obj.Id, obj.Priority));
}
}
}
class ObjectWithPriority
{
public int Id { get; set; }
public int Priority { get; set; }
}
I'm expecting the IEnumerable in the Main method to contain objects with modified priorities. However, all of them have the default value 0.
Here is the log:
Set priority of object #1 to 10
Set priority of object #2 to 20
Set priority of object #3 to 30
Object #1 has priority 0
Object #2 has priority 0
Object #3 has priority 0
What is the reason for suche behavior and what should I change here in order to get my priorities working?
When you do this:
var objs = new[] { 1, 2, 3 }.Select(i => new ObjectWithPriority() { Id = i });
You're simply creating a lazily evaluated iterator, this doesn't allocate an array/list to store the ObjectWithPriorty you project. Each time you enumerate the iterator, it will iterate the values again and project an ObjectWithPriority for each iteration, but will discard them.
What you want to do is materialize the query before you pass it, so later you'll actually modifying the already allocated list/array. This can be achieved using Enumerable.ToList or Enumerable.ToArray:
public static IEnumerable<ObjectWithPriority> CreateObjectsWithPriorities()
{
var objs = new[] { 1, 2, 3 }.Select(i => new ObjectWithPriority() { Id = i })
.ToList();
ApplyPriorities(objs);
return objs;
}
You could additional use Enumerable.Range instead of allocating a fixed size array, which will lazily project the numbers as requested:
var objs = Enumerable.Range(1, 3).Select(i => new ObjectWithPriority { Id = i })
.ToList();
To better understand what is happening in your program, you should think of this expression
var objs = new[] { 1, 2, 3 }.Select(i => new ObjectWithPriority() { Id = i });
as a query, not as a sequence/list/collection of objects.
But it is obvious from your code that in this particular program you don't need a query. You need a collection that has a finite number of objects and that returns the same objects every time you loop through it using foreach.
So a decent thing to do would be to use ICollection<ObjectWithPriority> instead of IEnumerable<ObjectWithPriority>. This would better represent the program's logic and help avoid some mistakes/misinterpretations like the one you stumbled upon.
The code could be modified as follows:
public static ICollection<ObjectWithPriority> CreateObjectsWithPriorities()
{
IEnumerable<ObjectWithPriority> queryThatProducesObjectsWithPriorities = new[] { 1, 2, 3 }.Select(i => new ObjectWithPriority() { Id = i }); // just for clarification
ICollection<ObjectWithPriority> objectsWithPriorities = queryThatProducesObjectsWithPriorities.ToList();
ApplyPriorities(objectsWithPriorities);
return objectsWithPriorities;
}
static void ApplyPriorities(ICollection<ObjectWithPriority> objs)
{
foreach (var obj in objs)
{
obj.Priority = obj.Id * 10;
Console.WriteLine(String.Format("Set priority of object #{0} to {1}", obj.Id, obj.Priority));
}
}
In Addition to the Answer of Yuval Itzchakov:
If you want to lazy-load the priority of your objects you could:
Define your ApplyPriorities() Method just for one object and use it in the select-Method OR add a delegate to your ObjectWithPriority class wich calculates the Priority like shown in the code below:
class ObjectWithPriority
{
public int Id { get; set; }
private int? priority;
public int Priority {
get
{
return (priority.HasValue ? priority.Value : (priority = PriorityProvider(this)).Value);
}
set { priority = value; }
}
Func<ObjectWithPriority, int> PriorityProvider { get; set; }
public ObjectWithPriority(Func<ObjectWithPriority, int> priorityProvider = null)
{
PriorityProvider = priorityProvider ?? (obj => 10 * obj.Id);
}
}
Problem: I have 2 kinds of objects, lets call them Building and Improvement. There are roughly 30 Improvement instances, while there can be 1-1000 Buildings. For each combination of Building and Improvement, I have to perform some heavy calculation, and store the result in a Result object.
Both Buildings and Improvements can be represented by an integer ID.
I then need to be able to:
Access the Result for a given Building and Improvement efficiently (EDIT: see comment further down)
Perform aggregations on the Results for all Improvements for a given Building, like .Sum() and .Average()
Perform the same aggregations on the Results for all Buildings for a given Improvement
This will happen on a web-server back-end, so memory may be a concern, but speed is most important.
Thoughts so far:
Use a Dictionary<Tuple<int, int>, Result> with <BuildingID, ImprovementID> as key. This should give me speedy inserts and single lookups, but I am concerned about .Where() and .Sum() performance.
Use a two-dimensional array, with one dimension for BuildingIDs and one for ImprovementIDs, and the Result as value. In addition, build two Dictionary<int, int> that map BuildingIDs and ImprovementIDs to their respective array row/column indexes. This could potentially mean max 1000+ Dictionarys, will this be a problem?
Use a List<Tuple<int, int, Result>>. I think this may be the least efficient, with O(n) inserts, though I could be wrong.
Am I missing an obvious better option here?
EDIT: Turns out it is only the aggregated values (per Building and per Improvement) I am interested in; see my answer.
Generally, the Dictionary is most lookup efficent. The both lookup efficency and manipulation efficency is constant O(1), when accessed via key. This will help for access, the first point.
In the second and third you need to walk through all of the items O(n), so there is no way to speed it except you want to walk them through in specified order O(n*n) - then you can use SortedDictionray O(n), but you compromise the lookup and manipulation efficency (O(log n)).
So I would go with the 1st solution you post.
You could use a "dictionary of dictionaries" to hold the Result data, for example:
// Building ID ↓ ↓ Improvement ID
var data = new Dictionary<int, Dictionary<int, Result>>();
This would let you quickly find the improvements for a particular building.
However, finding the buildings that contain a particular improvement would require iterating over all the buildings. Here's some sample code:
using System;
using System.Linq;
using System.Collections.Generic;
namespace Demo
{
sealed class Result
{
public double Data;
}
sealed class Building
{
public int Id;
public int Value;
}
sealed class Improvement
{
public int Id;
public int Value;
}
class Program
{
void run()
{
// Building ID ↓ ↓ Improvement ID
var data = new Dictionary<int, Dictionary<int, Result>>();
for (int buildingKey = 1000; buildingKey < 2000; ++buildingKey)
{
var improvements = new Dictionary<int, Result>();
for (int improvementKey = 5000; improvementKey < 5030; ++improvementKey)
improvements.Add(improvementKey, new Result{ Data = buildingKey + improvementKey/1000.0 });
data.Add(buildingKey, improvements);
}
// Aggregate data for all improvements for building with ID == 1500:
int buildingId = 1500;
var sum = data[buildingId].Sum(result => result.Value.Data);
Console.WriteLine(sum);
// Aggregate data for all buildings with a given improvement.
int improvementId = 5010;
sum = data.Sum(improvements =>
{
Result result;
return improvements.Value.TryGetValue(improvementId, out result) ? result.Data : 0.0;
});
Console.WriteLine(sum);
}
static void Main()
{
new Program().run();
}
}
}
To speed up the second aggregation (for summing data for all improvements with a given ID) we can use a second dictionary:
// Improvment ID ↓ ↓ Building ID
var byImprovementId = new Dictionary<int, Dictionary<int, Result>>();
You would have an extra dictionary to maintain, but it's not too complicated. Having a few nested dictionaries like this might take too much memory though - but it's worth considering.
As noted in the comments below, it would be better to define types for the IDs and also for the dictionaries themselves. Putting that together gives:
using System;
using System.Linq;
using System.Collections.Generic;
namespace Demo
{
sealed class Result
{
public double Data;
}
sealed class BuildingId
{
public BuildingId(int id)
{
Id = id;
}
public readonly int Id;
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as BuildingId;
if (other == null)
return false;
return this.Id == other.Id;
}
}
sealed class ImprovementId
{
public ImprovementId(int id)
{
Id = id;
}
public readonly int Id;
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as ImprovementId;
if (other == null)
return false;
return this.Id == other.Id;
}
}
sealed class Building
{
public BuildingId Id;
public int Value;
}
sealed class Improvement
{
public ImprovementId Id;
public int Value;
}
sealed class BuildingResults : Dictionary<BuildingId, Result>{}
sealed class ImprovementResults: Dictionary<ImprovementId, Result>{}
sealed class BuildingsById: Dictionary<BuildingId, ImprovementResults>{}
sealed class ImprovementsById: Dictionary<ImprovementId, BuildingResults>{}
class Program
{
void run()
{
var byBuildingId = CreateTestBuildingsById(); // Create some test data.
var byImprovementId = CreateImprovementsById(byBuildingId); // Create the alternative lookup dictionaries.
// Aggregate data for all improvements for building with ID == 1500:
BuildingId buildingId = new BuildingId(1500);
var sum = byBuildingId[buildingId].Sum(result => result.Value.Data);
Console.WriteLine(sum);
// Aggregate data for all buildings with a given improvement.
ImprovementId improvementId = new ImprovementId(5010);
sum = byBuildingId.Sum(improvements =>
{
Result result;
return improvements.Value.TryGetValue(improvementId, out result) ? result.Data : 0.0;
});
Console.WriteLine(sum);
// Aggregate data for all buildings with a given improvement using byImprovementId.
// This will be much faster than the above Linq.
sum = byImprovementId[improvementId].Sum(result => result.Value.Data);
Console.WriteLine(sum);
}
static BuildingsById CreateTestBuildingsById()
{
var byBuildingId = new BuildingsById();
for (int buildingKey = 1000; buildingKey < 2000; ++buildingKey)
{
var improvements = new ImprovementResults();
for (int improvementKey = 5000; improvementKey < 5030; ++improvementKey)
{
improvements.Add
(
new ImprovementId(improvementKey),
new Result
{
Data = buildingKey + improvementKey/1000.0
}
);
}
byBuildingId.Add(new BuildingId(buildingKey), improvements);
}
return byBuildingId;
}
static ImprovementsById CreateImprovementsById(BuildingsById byBuildingId)
{
var byImprovementId = new ImprovementsById();
foreach (var improvements in byBuildingId)
{
foreach (var improvement in improvements.Value)
{
if (!byImprovementId.ContainsKey(improvement.Key))
byImprovementId[improvement.Key] = new BuildingResults();
byImprovementId[improvement.Key].Add(improvements.Key, improvement.Value);
}
}
return byImprovementId;
}
static void Main()
{
new Program().run();
}
}
}
Finally, here's a modified version which determines the time it takes to aggregate data for all instances of a building/improvement combination for a particular improvement and compares the results for dictionary of tuples with dictionary of dictionaries.
My results for a RELEASE build run outside any debugger:
Dictionary of dictionaries took 00:00:00.2967741
Dictionary of tuples took 00:00:07.8164672
It's significantly faster to use a dictionary of dictionaries, but this is only of importance if you intend to do many of these aggregations.
using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
namespace Demo
{
sealed class Result
{
public double Data;
}
sealed class BuildingId
{
public BuildingId(int id)
{
Id = id;
}
public readonly int Id;
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as BuildingId;
if (other == null)
return false;
return this.Id == other.Id;
}
}
sealed class ImprovementId
{
public ImprovementId(int id)
{
Id = id;
}
public readonly int Id;
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as ImprovementId;
if (other == null)
return false;
return this.Id == other.Id;
}
}
sealed class Building
{
public BuildingId Id;
public int Value;
}
sealed class Improvement
{
public ImprovementId Id;
public int Value;
}
sealed class BuildingResults : Dictionary<BuildingId, Result>{}
sealed class ImprovementResults: Dictionary<ImprovementId, Result>{}
sealed class BuildingsById: Dictionary<BuildingId, ImprovementResults>{}
sealed class ImprovementsById: Dictionary<ImprovementId, BuildingResults>{}
class Program
{
void run()
{
var byBuildingId = CreateTestBuildingsById(); // Create some test data.
var byImprovementId = CreateImprovementsById(byBuildingId); // Create the alternative lookup dictionaries.
var testTuples = CreateTestTuples();
ImprovementId improvementId = new ImprovementId(5010);
int count = 10000;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i)
byImprovementId[improvementId].Sum(result => result.Value.Data);
Console.WriteLine("Dictionary of dictionaries took " + sw.Elapsed);
sw.Restart();
for (int i = 0; i < count; ++i)
testTuples.Where(result => result.Key.Item2.Equals(improvementId)).Sum(item => item.Value.Data);
Console.WriteLine("Dictionary of tuples took " + sw.Elapsed);
}
static Dictionary<Tuple<BuildingId, ImprovementId>, Result> CreateTestTuples()
{
var result = new Dictionary<Tuple<BuildingId, ImprovementId>, Result>();
for (int buildingKey = 1000; buildingKey < 2000; ++buildingKey)
for (int improvementKey = 5000; improvementKey < 5030; ++improvementKey)
result.Add(
new Tuple<BuildingId, ImprovementId>(new BuildingId(buildingKey), new ImprovementId(improvementKey)),
new Result
{
Data = buildingKey + improvementKey/1000.0
});
return result;
}
static BuildingsById CreateTestBuildingsById()
{
var byBuildingId = new BuildingsById();
for (int buildingKey = 1000; buildingKey < 2000; ++buildingKey)
{
var improvements = new ImprovementResults();
for (int improvementKey = 5000; improvementKey < 5030; ++improvementKey)
{
improvements.Add
(
new ImprovementId(improvementKey),
new Result
{
Data = buildingKey + improvementKey/1000.0
}
);
}
byBuildingId.Add(new BuildingId(buildingKey), improvements);
}
return byBuildingId;
}
static ImprovementsById CreateImprovementsById(BuildingsById byBuildingId)
{
var byImprovementId = new ImprovementsById();
foreach (var improvements in byBuildingId)
{
foreach (var improvement in improvements.Value)
{
if (!byImprovementId.ContainsKey(improvement.Key))
byImprovementId[improvement.Key] = new BuildingResults();
byImprovementId[improvement.Key].Add(improvements.Key, improvement.Value);
}
}
return byImprovementId;
}
static void Main()
{
new Program().run();
}
}
}
Thanks for the answers, the test code was really informative :)
The solution for me turned out to be to forgo LINQ, and perform aggregation manually directly after the heavy calculation, as I had to iterate over each combination of Building and Improvement anyway.
Also, I had to use the objects themselves as keys, in order to perform calculations before the objects were persisted to Entity Framework (i.e. their IDs were all 0).
Code:
public class Building {
public int ID { get; set; }
...
}
public class Improvement {
public int ID { get; set; }
...
}
public class Result {
public decimal Foo { get; set; }
public long Bar { get; set; }
...
public void Add(Result result) {
Foo += result.Foo;
Bar += result.Bar;
...
}
}
public class Calculator {
public Dictionary<Building, Result> ResultsByBuilding;
public Dictionary<Improvement, Result> ResultsByImprovement;
public void CalculateAndAggregate(IEnumerable<Building> buildings, IEnumerable<Improvement> improvements) {
ResultsByBuilding = new Dictionary<Building, Result>();
ResultsByImprovement = new Dictionary<Improvement, Result>();
for (building in buildings) {
for (improvement in improvements) {
Result result = DoHeavyCalculation(building, improvement);
if (ResultsByBuilding.ContainsKey(building)) {
ResultsByBuilding[building].Add(result);
} else {
ResultsByBuilding[building] = result;
}
if (ResultsByImprovement.ContainsKey(improvement)) {
ResultsByImprovement[improvement].Add(result);
} else {
ResultsByImprovement[improvement] = result;
}
}
}
}
}
public static void Main() {
var calculator = new Calculator();
IList<Building> buildings = GetBuildingsFromRepository();
IList<Improvement> improvements = GetImprovementsFromRepository();
calculator.CalculateAndAggregate(buildings, improvements);
DoStuffWithResults(calculator);
}
I did it this way because I knew exactly which aggregations I wanted; if I required a more dynamic approach I would probably have gone with something like #MatthewWatson's Dictionary of Dictionaries.
I have objects in Autocad drawing with property named Base. I am trying to find all objects in that drawing with Base property has a specific string value such as "Pipe".
I can iterate objects in the drawing and get all object ids. Then I get all properties of object with that Id and check if property named Base = "Pipe".
Iteration performance is not good enough. Is there any way to directly get object ids that has property named Base = "Pipe"?
Here is how I iterate through all objects:
List<ObjectId> ObjectIds = new List<ObjectId>();
foreach (Document Document in Documents)
{
Database Database = Document.Database;
using (Transaction Transaction = Database.TransactionManager.StartTransaction())
{
for (long i = Database.BlockTableId.Handle.Value; i < Database.Handseed.Value; i++)
{
ObjectId Id;
if (Database.TryGetObjectId(new Handle(i), out Id))
{
ObjectIds.Add(Id);
}
}
Transaction.Commit();
}
}
And here is how I get all properties of the objects in my ObjectIds collection.
public static DataLinksManager DataLinks
{
get
{
if (null == _DataLinks)
{
StringCollection Coll = Autodesk.ProcessPower.DataLinks.DataLinksManager.GetLinkManagerNames();
if (Coll.Count > 0)
{
if (Coll[0] != string.Empty)
{
_DataLinks = Autodesk.ProcessPower.DataLinks.DataLinksManager.GetManager(Coll[0]);
}
}
}
return _DataLinks;
}
}
private static DataLinksManager _DataLinks;
foreach(var Id in ObjectIds)
{
List<KeyValuePair<string, string>> Properties = DataLinks.GetAllProperties(Id, true);
// I check existence of my property and if so its value.
}
In case anyone needs, here is the code that is solution to my problem. The trick is the Iterate method. This is based on this article by Philippe Leefsma. What I add to this method is a list of ObjectClass properties of found ObjectId instances. My sample dawing has around 8500 ObjectIds. However what I'm interested in is objects with base classes acppasset and acppdynamicasset and count of such objects is 90.
using Autodesk.AutoCAD.ApplicationServices;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
public static void GetObjects()
{
List<KeyValuePair<string, ObjectId>> ObjectIds = new List<KeyValuePair<string, ObjectId>>();
List<string> Filter = new List<string>() { "acppasset", "acppdynamicasset" };
foreach (Document Document in this.Documents)
{
ObjectIdCollection Ids = this.Iterate(Document, Filter);
if (null != Ids) foreach (var Id in Ids.OfType<ObjectId>()) ObjectIds.Add(new KeyValuePair<string, ObjectId>(System.IO.Path.GetFileNameWithoutExtension(Document.Name), Id));
}
this.Results = new Dictionary<string, List<List<KeyValuePair<string, string>>>>();
foreach (var Id in ObjectIds)
{
try
{
var Properties = this.GetObject(Id.Value);
if (null == Properties) continue;
var Base = Properties.Where(x => x.Key == "Base").FirstOrDefault();
if (string.IsNullOrWhiteSpace(Base.Value)) continue;
if (!this.Results.ContainsKey(Base.Value)) this.Results.Add(Base.Value, new List<List<KeyValuePair<string, string>>>());
this.Results[Base.Value].Add(Properties);
} catch { }
}
}
public ObservableCollection<Document> Documents { get; set; }
public ObjectIdCollection Iterate(Document Document, List<string> Filter = null)
{
ads_name Instance = new ads_name();
Database Database = Document.Database;
ObjectIdCollection ValidIds = new ObjectIdCollection();
// Get the last handle in the Database
Handle Handseed = Database.Handseed;
// Copy the handseed total into an efficient raw datatype
long HandseedTotal = Handseed.Value;
for (long i = 1; i < HandseedTotal; ++i)
{
string Handle = Convert.ToString(i, 16);
int Result = acdbHandEnt(Handle, ref Instance);
if (Result != 5100) continue; // RTNORM
ObjectId Id = new ObjectId(Instance.a);
if (!Id.IsValid) continue;
try
{
if (null != Filter)
{
if (!Filter.Contains(Id.ObjectClass.Name.ToLower())) continue;
}
using (DBObject DBObject = Id.Open(OpenMode.ForRead, false))
{
ValidIds.Add(Id);
DBObject.Dispose();
}
} catch { }
}
return ValidIds;
}
public List<KeyValuePair<string, string>> GetObject(ObjectId Id)
{
if (Command.DataLinks != null) try { return Command.DataLinks.GetAllProperties(Id, true); } catch { return null; }
return null;
}
public static DataLinksManager DataLinks
{
get
{
if (null == _DataLinks)
{
StringCollection Coll = Autodesk.ProcessPower.DataLinks.DataLinksManager.GetLinkManagerNames();
if (Coll.Count > 0)
{
if (Coll[0] != string.Empty)
{
_DataLinks = Autodesk.ProcessPower.DataLinks.DataLinksManager.GetManager(Coll[0]);
}
}
}
return _DataLinks;
}
}
private static DataLinksManager _DataLinks;
public Dictionary<string, List<List<KeyValuePair<string, string>>>> Results { get; set; }
The slow performance here is because it attempts to read all the objects and check if it contains any attribute. As far as I know, the attributes exist only for block references(inserts). So if selection filters are used, we could get direct access to only those records based on the filter criteria.
I found a pretty easy example here using selection filter that selects all blocks with a particular name.
Copying a part of that code for reference. This selects only the block references. You can iterate from here.
TypedValue[] filterlist = new TypedValue[1];
filterlist[0] = new TypedValue(0, "INSERT");
SelectionFilter filter = new SelectionFilter(filterlist);
PromptSelectionResult selRes = ed.SelectAll(filter);
if (selRes.Value.Count != 0)
{
SelectionSet set = selRes.Value;
foreach (ObjectId id in set.GetObjectIds())
{
BlockReference oEnt = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
//do something with oEnt..;
}
}
If you can add complexities to your filter, you will need to iterate only a very small set.
I have an Interface [BindControls] which takes data from GUI and store it into a list „ieis”.
After that, Into another class, which sends this data through WebServices, I want to take this data from „ieis” and put it into required by WS Class fields (bottom is a snippet of code)
This is the interface:
void BindControls(ValidationFrameBindModel<A.B> model)
{
model.Bind(this.mtbxTax, (obj, value) =>
{
var taxa = TConvertor.Convert<double>((string)value, -1);
if (taxa > 0)
{
var ieis = new List<X>();
var iei = new X
{
service = new ServiceInfo
{
id = Constants.SERVICE_TAX
},
amount = tax,
currency = new CurrencyInfo
{
id = Constants.DEFAULT_CURRENCY_ID
}
};
ieis.Add(iei);
}
},"Tax");
}
This is the intermediate property:
//**********
class A
{
public B BasicInfo
{
get;
set;
}
class B
{
public X Tax
{
get;
set;
}
}
}
//***********
This is the class which sends through WS:
void WebServiceExecute(SomeType someParam)
{
//into ‚iai’ i store the data which comes from interface
var iai = base.Params.FetchOrDefault<A>( INFO, null);
var convertedObj = new IWEI();
//...
var lx = new List<X>();
//1st WAY: I tried to put all data from ‚Tax’into my local list ‚lx’
//lx.Add(iai.BasicInfo.Tax); - this way is not working
//2nd WAY: I tried to put data separately into ‚lx’
var iei = new X
{
service = new ServiceInfo
{
id = iai.BasicInfo.Tax.service.id
},
amount = iai.BasicInfo.Tax.amount,
currency = new CurrencyInfo
{
id = iai.BasicInfo.Tax.currency.id
}
};
lx.Add(iei);
// but also is not working
Can you help me please to suggest how to implement a way that will fine do the work (take data from ‚ieis’ and put her into ‚lx’).
Thank you so much
As noted in my comment, it looks like iai.BasicInfo.Tax is null, once you find out why that is null your original Add() (#1) will work.