List, Dictionary or something else?

J

jp2msft

I'm pulling records from or database by dates, and there are multiple records
for each part number.

The part numbers are being stored as strings in a List in a custom Employee
class:

List<string> Parts = new List<string>();

If a part number is not already in the list, a new entry is created for an
employee that gets credited this part number:

foreach (EmployeeRecord emp in Employee) {
if (emp.Parts.Contains(strPartNum) == false) {
emp.Parts.Add(strPartNum);
emp.TotalParts += 1;
}
}

This seems very efficient to me.

Now, I have been asked to modify my collection to include the Date that this
part number was recorded.

Does anyone know of a good way to do this?

I've considered using a Dictionary entry using a part number as the key and
the data as the value (or vice versa), but I don't know enough about how to
use these Dictionary entries in a way that is not an actual dictionary.

I tried creating a special class to hold a part number and a date, then
adding that to my List, but then there does not seem to be a way to parse the
List to see if a part number has already been added.

I'm working with Visual Studio 2005 (.NET Framework 2.0).

What other techniques are there that I could look into? Are there better
(i.e. faster) techniques? I don't mind writing more code, but I do to go
through the data quickly.
 
N

Nicholas Paldino [.NET/C# MVP]

First, it seems that the TotalParts property shouldn't be assigned to.
Why not get the count of the parts from the collection exposed by Parts? It
allows someone to set the number of parts which would be out of line with
the items exposed by the Parts collection.

As for your specific problem, I would have the part numbers in a
separate collection, not attached to the part or employee. The date the
part number was entered doesn't seem to fit into the domain of the parts (I
don't know the specific domain, so I can't say), but it almost seems like an
audit trail of some sort, and is generally not exposed as part of the entity
in a business model.

I would keep the collection separately, keyed on the part number.
 
J

jp2msft

Thanks for the help.

Now... Where's a good place to get a quick understanding of the difference
between this business model and the others? (I suppose they are application
layer and data layer)

These terms are tossed around from time to time, and I feel like I missed
this chapter.

Nicholas Paldino said:
First, it seems that the TotalParts property shouldn't be assigned to.
Why not get the count of the parts from the collection exposed by Parts? It
allows someone to set the number of parts which would be out of line with
the items exposed by the Parts collection.

As for your specific problem, I would have the part numbers in a
separate collection, not attached to the part or employee. The date the
part number was entered doesn't seem to fit into the domain of the parts (I
don't know the specific domain, so I can't say), but it almost seems like an
audit trail of some sort, and is generally not exposed as part of the entity
in a business model.

I would keep the collection separately, keyed on the part number.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

jp2msft said:
I'm pulling records from or database by dates, and there are multiple
records
for each part number.

The part numbers are being stored as strings in a List in a custom
Employee
class:

List<string> Parts = new List<string>();

If a part number is not already in the list, a new entry is created for an
employee that gets credited this part number:

foreach (EmployeeRecord emp in Employee) {
if (emp.Parts.Contains(strPartNum) == false) {
emp.Parts.Add(strPartNum);
emp.TotalParts += 1;
}
}

This seems very efficient to me.

Now, I have been asked to modify my collection to include the Date that
this
part number was recorded.

Does anyone know of a good way to do this?

I've considered using a Dictionary entry using a part number as the key
and
the data as the value (or vice versa), but I don't know enough about how
to
use these Dictionary entries in a way that is not an actual dictionary.

I tried creating a special class to hold a part number and a date, then
adding that to my List, but then there does not seem to be a way to parse
the
List to see if a part number has already been added.

I'm working with Visual Studio 2005 (.NET Framework 2.0).

What other techniques are there that I could look into? Are there better
(i.e. faster) techniques? I don't mind writing more code, but I do to go
through the data quickly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top