问题
I am having a Form where
Table SelectedItems Table AvailableItems
RecordID CheckBoxID CheckBoxID Description
1 1 1 'Tomatoes'
1 2 2 'Potatoes'
1 4 3 'Mangoes'
2 1 4 'Apples'
2 2
- Now I have to edit the SelectedItems for RecordID 1
- For that purpose I made a CheckBox list and populate it with AvaialbeItems
- Then I looped and checked the SelectedItems for RecordID 1
- User selects some new rows from the available list (along with old ones, he may deselect some as well)
Now I want to update the selected records, problem being I want to know only new records to insert.
Both Tables are being returned as Business Object Lists<> , How do I compare the lists and extract only changed/new rows ?
Please advice me. I am using webforms on VSS 2005.
回答1:
When you select record(s), could you update the business object(s) and dirty them? What I mean is create a class variable that will serve as a flag that you can check to see if it was changed or new. The select new rows (or old) I would think fire an event that you can use to modify the selected row's business object.
Then when you go through your Lists you just need to check the business object flag that yields the status of change/new.
回答2:
If the system is already on production server and running stably, please ignore my answer. If it's still under planning/designing/programming stage, I think probably a slight DB change will make everything much more easier.
Table SelectedItems
RecordID CheckBoxID IsSelected
1 1 true
1 2 true
1 3 false
1 4 true
That is, for a certain RecordID and CheckBoxID, you don't need to query the table to find if there is a corresponding record(which means it's selected). In the new design, there is always such a record, with a IsSelected
status.
Meanwhile, this design is also very easy for maintanance in your web pages. What you change is just IsSelected
value for a certain row, you don't need to care about "Oh, do I need to delete one row or add one?" "Is the data of this row dirty or not?"...things become easier.
来源:https://stackoverflow.com/questions/3581791/changed-rows-from-list-of-objects