Handling Many to Many - Entity Framework
I have an MVC app I am building as a learning project. I have a table with Payees, and a table with Categories. Certain Payees can make transactions of certain categories. For example, Payee A can make payments to Categories A, B and C. Payee B can maybe payments to categories B, C and D.
So, I have a PayeeCategory linking table, with a PayeeId and a CategoryId.
So see the categories the payee can make transactions against, it's a simple Select from PayeeCategory where PayeeId = this.PayeeId. And I join to the Category table to get the category names.
In the view, when I edit a Payee, I need to display a list of all categories, and and allow the user to select or unselect the categories that the current Payee is associated to.
So, in my ViewMode开发者_Go百科l, I'd have a list of categorySelectableItems, which has maybe the CategoryId, the DisplayName and a Selected boolean field. To get the data, I need to Select from Categories, LEFT JOIN PayeeCategory, and where PayeeCategoryId is null, set the Selected to false, else true.
How would I do that ina single Linq statement?
Then, when the data comes back... how would I save the data? Would I have to delete all the relationships, and then re-add them based on the List<> values I get back?
Hope someone can guide me.
This was answered in a different question.
Linq query assistance
精彩评论