none
update a ObservableCollection using Linq

    Question

  • Hi everyone.

     i want to update a ObservableCollection using linq but i can`t. Please tell me how to and give a example.

    note (i am work in WPF)

    thnk`s for help

    Tuesday, June 28, 2011 1:56 PM

Answers

  • Don't use LINQ.

    The Q in LINQ stands for query.  LINQ is specifically designed for read-only operations.  Although the extension methods that LINQ uses take delegates as parameters and it is possible to execute virtually any code inside those delegates (e.g., edit the iterator item), this is considered a very bad practice.  Executing a LINQ query should not have any side effects on the target enumeration.

    Evan

    Tuesday, June 28, 2011 3:49 PM

All replies

  • What exactly do you want to do? Update can mean anything from setting the first item of the ObservableCollection to deleting a few items conditionally.

    Specify what you need to do and we can help you out


    Please mark this as answer or vote as helpful if it solved your problem
    Tuesday, June 28, 2011 2:21 PM
  • Edit and Delete some item in  ObservableCollection  using Linq

    Tuesday, June 28, 2011 2:29 PM
  • Don't use LINQ.

    The Q in LINQ stands for query.  LINQ is specifically designed for read-only operations.  Although the extension methods that LINQ uses take delegates as parameters and it is possible to execute virtually any code inside those delegates (e.g., edit the iterator item), this is considered a very bad practice.  Executing a LINQ query should not have any side effects on the target enumeration.

    Evan

    Tuesday, June 28, 2011 3:49 PM
  • Evan is absolutely correct - 

     

    That being said, you can use LINQ to find the objects inside of the collection to update, then update them individually.  It depends on what you're updating.  For example, if your OC contains a collection of "MyClass" instances, and you need to increment some value, you could do:

     

    // Find items to update
    var toUpdate = myObservableCollection.Where(item => item.SomeCondition == "Foo");
    foreach(var item in toUpdate)
      item.Bar++; // Increment your value...
    
    

    However, you're not using LINQ to do the actual update - just to find (query) the items...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, June 28, 2011 4:26 PM