【问题标题】:Foreach loop through ObservableCollectionForeach 循环通过 ObservableCollection
【发布时间】:2010-01-07 10:35:28
【问题描述】:

在 WPF 应用程序中,我使用 LINQ 查询从 ObservableCollection 获取值并更新某些对象的状态:

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;
           ...
         }

实际上,xyID 是此 ObservableCollection 中的唯一字段,其字符串值对应于 XAML 中的对象名称(这些对象源自客户控件 MyCustControl)。

我想做的是:

(1) 遍历_ShAvaQuCollection 中的所有记录,并使用xyID 字段引用每个特定对象,使用shava 作为对每个记录的查询结果:

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

(2) 并使用此记录中的其他值更新对象的状态,例如:

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

除了所有这些操作都可以很好地工作,但我不能将它结合到一个有效的迭代方法中,就像这样(这只是一个想法,因为我没有设法获得可编译的代码):

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

请帮助我在 foreach 循环内的最后一段代码中组织这样的迭代。我在理解如何对循环内集合的每个特定记录进行查询时遇到问题。

我描述了以上所有内容,只是为了说明我打算在这个循环中处理此类查询的结果。

【问题讨论】:

  • 抱歉,我的问题可能不是很准确。事实上,这段代码编译OK。我在查询循环内的每个特定记录时遇到问题。谢谢,我现在将纠正我的问题。

标签: c# wpf linq generics foreach


【解决方案1】:

看来您需要获取ShowAvaQu 对象的序列,使用FindName 将其转换为MyCustControl 的序列,并根据每个ShowAvaQu 的状态更新每个控件的状态。

首先,让我们将状态和控件关联起来:

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

然后,迭代该序列,根据需要更新状态:

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;
        //...
    }
}

看起来对吗?

【讨论】:

  • 是的,确实如此。我试过了,它工作正常。对我来说,这不仅是另一种做事方式,而且是一段可供学习的代码。谢谢,布莱恩! +1
【解决方案2】:

看来我设法做到了(并且有效):

 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;
           }
        }
    }

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2013-08-19
    相关资源
    最近更新 更多