views:

280

answers:

2

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.

A: 

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;
           }
        }
    }
rem
+1  A: 

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?

Bryan Watts
Yes, it does. I tried it and it works OK. For me, it is not only yet another way of doing things, but also the piece of code to learn from.Thanks, Bryan! +1
rem