Here's the cool part. And, EntitySet s let you specify delegates to be called whenever an item is added to or removed from its collection. So, create two delegate methods to handle this synchronization:. Then, construct the EntitySet by passing in these two delegate methods. Since the methods are instance methods, you'll need to do this in Category 's constructor:. To demonstrate adding new 1:M relationships, add a couple of new books without a category.
Notice that we never set the Category on the books directly, but the synchronization code we added takes care of that for us. So now, if you view the details for the books, you'll see their Category is set to WPF :.
Therefore, if you take a book that's in one category and add it to a different category, you are effectively moving it to that new category even though you didn't explicitly ask for this. When we remove a book from a category, our OnBookRemoved delegate clears the Category property from that Book instance. We, for some reason, have two copies of Programming Ruby , so let's remove the Programming Ruby 1.
Finally, we can complete our M:M relationships. These are a little trickier because we want to keep our Join table BookAuthor out of our public interface so callers can work directly with Book s and Author s and not have to care how they're joined together.
If you're coding along, then you already added logic into BookAuthor so that if either side updates a BookAuthor relationship, it handles synchronizing both the Book and the Author for you. What remains is providing a mechanism for users to set a Book 's authors, and an Author 's books. And, we created a public Authors property that serves as a proxy, pulling this book's authors from BookAuthors :.
The problem with this approach is we now have no way to tell when a caller adds or removes Author s from our Book. What we need is something akin to the EntityRef delegates for OnAdd and OnRemove so that we can perform our synchronization whenever a caller adds or removes an author.
Since our data is not in an EntryRef, we need another way to get notifications when an author is added or deleted. We can do this by making Authors return an ObservableCollection that will notify us when changes are made to it:. The second line authors. This is the delegate method we registered to be notified of all changes made to our Authors collection.
Create the method to respond to the Add and Remove events, and use its NotifyCollectionChangedEventArgs to gain access to the items that were added e. NewItems or removed e. OldItems :. If you've been coding along, then you already did this when you added logic to BookAuthor 's set methods for Book to add the new BookAuthor instance to the Book :.
The way this works is that when you attach add the BookAuthor instance to your Book , LINQ to SQL notices the change because it's monitoring Book for changes , and so it will automatically insert this attached BookAuthor record into the database the next time you call SubmitChanges.
Voila, a new BookAuthor relationship. Step 1 calls a RemoveRecord method on the DataContext which we'll add below. Github: ranacseruet. This is indeed a very useful piece of information. I know this code is gonna help me famous in lab session tomorrow! Thnx a lot buddy! Nice Post Buddy…………. Keep It Up………………………. Thank you so much. Just a comment on style, instead of using:. Id ; nUser. RoleId; nUser. Username; nUser. Password; UDB. There is no built-in extension method to do this.
Although defining one is fairly straight forward. At the bottom of the post is a method I defined called Iterate. It can be used like so. Again, this is somebody else's solution. But I've compiled the code into a small library, and use it fairly regularly.
I'm going to paste his code here, for the off chance that his site blog ceases to exist at some point in the future. No, LINQ doesn't support a manner of mass updating. Obviously this will not work with collections of structs or inbuilt types like integers or strings. Some people consider this is a comment, but for me is an answer, because the right way to do something wrong is not do it.
So, the answer for this question is in the question itself. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Asked 13 years ago. Active 2 months ago. Viewed k times. Is there a way to do the following using LINQ? Improve this question. Peter Mortensen Interesting question. Personally I prefer how you've got it above - far clearer what's going on! I came here looking for an answer to the same question, and decided that it was just as easy, less code, and easier to understand for future developers to just do it the way you did in you OP. Why would you want to do it in LINQ? This question asks for the wrong thing, the only correct answer is: don't use LINQ to modify the datasource — Tim Schmelter.
I'm voting to close this question as off-topic because almost all the answers to this question are actively harmful to new programmers' understanding of LINQ. Show 2 more comments. Active Oldest Votes.
While you can use a ForEach extension method, if you want to use just the framework you can do collection. ToList ; The ToList is needed in order to evaluate the select immediately due to lazy evaluation. Improve this answer. Amirhossein Mehrvarzi If collection was an ObservableCollection say, then changing items in place rather than creating a new list can be useful. IMHO, this is far less expressive than a simple foreach loop. The ToList is confusing because it is not used for anything but forcing evaluation that would otherwise be deferred.
The projection is also confusing because it's not used for its intended purpose; rather, it's used to iterate over the elements of the collection and allow access to a property so that it can be updated. The only question in my mind would be whether or not the foreach loop could benefit from parallelism using Parallel.
ForEach, but that's a different question. This answer is a worst practice. Never do this. See all the other comments for reasons why this is a bad idea.
You should never use a select to execute a side effect. The purpose of a select is to select a value, not to emulate a for each loop. Show 29 more comments. Adi Lester 24k 12 12 gold badges 88 88 silver badges bronze badges. SanthoshKumar: Use collection. CameronMacFarland: Of course it won't since structs are immutable.
But if you really want, you can do this: collection. This has the advantage over Cameron MacFarland's answer of updating the list in place, rather than creating a new list. Wow, this answer is really not useful. Creating a new collection just to be able to use a loop — Tim Schmelter. SimonTewsi Since it is a collection of objects, the list should be updated in place anyways.
0コメント