SolvedPrivate

Clear ObservableCollection item

Posted on 2014-01-03
3
49 Views
Last Modified: 2016-02-10
I have an Observable Collection Called _items. I use it for a chart for when I add series. Each series has an item  in the collection. I need to clear an item in the collection by selecting the series  on the chart which I am able to do and I get the series name. I am using Linq to iterate through each item in the collection and get that Item. With that  in mind how do I clear that entry. Any help would be greatly appreciated
       private void click_me(object sender, ChartItemClickEventArgs e)
        {
            var filteredData = new List<Item>();
//Get the name of the series to delete
            string legendLabel = e.DataSeries.LegendLabel;
            //string strconfirm = "Confirm Deletion of " + legendLabel;
            //MessageBox.Show(strconfirm);
//run though each item in the collection
            foreach (var item in _items)
            {
                if (item != null)
                {
                    if (item.Count > 0)
                    {
//find it
                        var map = viewchart.SeriesMappings.Where(v => legendLabel == item[0].seriesType).FirstOrDefault();
                        if (map == null)
                            return;
//Clear it here
                        
                    }

                }
            }
           

           
        }

Open in new window

0
Comment
Question by:r3nder
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39756165
Hi  r3nder;

This should do what you need.

//run though each item in the collection
// I will assume that _items is a collection of data type Item
Item map = null;
foreach (var item in _items)
{
    if (item != null)
    {
        if (item.Count > 0)
        {
            //find it
            map = viewchart.SeriesMappings
                           .Where(v => legendLabel == item[0].seriesType)
                           .FirstOrDefault();
                           
            if (map == null)
                return;
            //Clear it here
            // Can't remove item while you are iterating through the list              
            // So do it outside the foreach loop
        }
    }
}

_items.Remove(map);

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 39756382
I was trying to  remove it in the find :) thanks man
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39757367
Not a problem, glad to help out.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question