简体   繁体   中英

Foreach loop through ObservableCollection

In WPF app I use LINQ query to get values from ObservableCollection and update the state of some objects:

var shava = from clc in _ShAvaQuCollection where clc.xyID == "x02y02" select clc;

        switch (shava.First().Status)
        {
            case 1:
                x02y02.Status = MyCustControl.Status.First;
                break;
            case 2:
                x02y02.Status = MyCustControl.Status.Second;
                break;
           ...
         }

In fact, xyID is unique field in this ObservableCollection and its string value corresponds to names of objects in XAML (these objects are derived from a customer control MyCustControl ).

What I am trying to do is:

(1) to iterate through all of the records in _ShAvaQuCollection and, using xyID field, reference each particular objects, using shava as a result of query to each of the record:

MyCustControl sc = (MyCustControl)this.FindName(shava.First().xyID);

(2) and update the state of the object, using other values from this record, for example:

           switch (shava.First().Status)
        {
            case 1:
                sc.Status = MyCustControl.Status.First;
                break;
            case 2:
                sc.Status = MyCustControl.Status.Second;
                break;
           ...
         }  

All these actions apart work well, but I cannot combine it to a working iteration method, something like this (it is an idea only, because I didn't manage to get a compyleable code):

public void ReadingCollection(System.Collections.Generic.IEnumerable<ShowAvaQu> coll)
{
   foreach (var xyid in coll)
        {
            //do my actions (1) and (2)
        } 
}

Please, help me to organize such an iteration in this last piece of code inside the foreach loop. I'm experiencing problems with understanding how to make a query to each particular record of the collection inside the loop.

I described that all above just to make clear what I intend to do with results of such query inside this loop.

It looks like you need to take a sequence of ShowAvaQu objects, turn it into a sequence of MyCustControl by using FindName , and update the status of each control based on the status of each ShowAvaQu .

First, let's associate the statuses and controls:

var shavaControls =
    from shava in coll
    select new
    {
        Status = shava.Status,
        Control = (MyCustControl) this.FindName(shava.xyID)
    };

Then, iterate that sequence, updating the statuses as necessary:

foreach(var shavaControl in shavaControls)
{
    switch(shavaControl.Status)
    {
        case 1:
        shavaControl.Control.Status = MyCustControl.Status.First;
        break;
        case 2:
        shavaControl.Control.Status = MyCustControl.Status.Second;
        break;
        //...
    }
}

Does that look right?

It seems I managed to do this (and it works):

 public void ReadingCollection(System.Collections.Generic.IEnumerable<ShowAvaQu> coll)
   {
        foreach (var id in coll)
        {               
            MyCustControl sc = (MyCustControl)this.FindName(id.xyID);
            switch (id.Status)
            {
                case 1:
                    sc.Status = MyCustControl.Status.First;
                    break;
                case 2:
                    sc.Status = MyCustControl.Status.Second;
                    break;
                case 3:
                    sc.Status = MyCustControl.Status.Third;
                    break;
                default:
                    break;
           }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM