views:

231

answers:

1

I am new to C# and WPF, so please bear with me..

This isn't exactly a Master/Detail scenario, and could be why I am having problems, but generally, I am trying to figure out how to relate two sets of data that are not quite master/detail.

To keep it simple, let's say I have two view/viewmodels for

1) Person
ID, FirstName, LastName

2) Contact
ID, PhoneNumber

Now, I know normally you think of a Person having several contacts, and it would be something like this:

    Contact :  
ID, PersonID, PhoneNumber

but for this example, let's continue the way I am going...

I have another table, a join table, that has

PersonID
ContactID

Now, if both Bob and Mary have the same phone number, there are 2 entries in the join table

1 1
2 1

(assuming Bob is 1, Mary is 2, and 555-555-5555 is ID 1 for the Contact)

So, I build the view and viewmodel for the Person, no problem I build the view and viewmodel for the Contact, again no problem

Now, how, if I build a view that uses Person as the master and Contact as the detail, to limit the display of only those contacts for the selected person? And likewise, if I have another view of Contact #s and I want to see a list of people associated with that one selected number, how to do this as well.

It is probably something easy, but I cannot seem to figure it out.

Or, do I need to actually create an ObservableCollection in the Person viewmodel and also ObservableCollection in the Contact viewmodel?

Any help would be appreciated.

A: 

Many-to-many relationships, a database concept, are difficult to express in C#.

One way to implement this is:

  1. Have your Person object expose a collection of Contact objects that belong to that person.
  2. Have your Contact objects expose a collection of Person objects that share that set of contact information.

This allows your Person view and your Contact view to operate like standard master-details views.

dthrasher