Reputation: 10247
I have a List<customClass
>.
The custom class contains two members that, together, form a pseudo composite key.
I want to be able to write to each instance of the custom class multiple times ("stepwise population" -- a couple of members assigned values at one point, then a couple more members assigned values at a later point, etc.).
When saving data to this List<T
>, I want to be able to locate the particular instance I am about to partially populate, if it has already been instantiated.
For example, let's say the "pseudo composite key" is:
class.member1 == "a"
class.member2 == "42"
I want to search to see if there already is an instance of my custom class with those values and, if so, use it; otherwise, create a new instance of that class to add to the List<T
>.
Is there a way to locate that particular instance of the custom class so that I can then assign members to as-yet unassigned members of that instance?
The reason I need to do this is that at the time of saving values (before they "go away") some but probably not all of the values of many if not all of the custom class instances are known; the next time a save is made, other parts of those same class instances are known and must be added to existing class instances; etc.
Upvotes: 1
Views: 77
Reputation: 17213
try {
var obj = list.First(x => x.Property1 == "1" && x.Property2 == "a");
} catch {
// Not found
}
Upvotes: 1