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.

Answers


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?


Need Your Help

MySQL DATE_ADD() when DATE is null

mysql sql null dateadd

I have a DATETIME value "some_date" that can be null in my database. When I try the following query it returns nothing for those rows with date is null:

jQuery form submit query parameters

javascript jquery

I'm from Java and know nothing about jQuery, my application need to integrate with openid and i have to choose openid-selector as my client, it is a jQuery plugin that can send a form to server with

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.