C# 通过ObservableCollection进行Foreach循环
在WPF应用程序中,我使用LINQ查询从ObservableCollection获取值并更新某些对象的状态:C# 通过ObservableCollection进行Foreach循环,c#,wpf,linq,generics,foreach,C#,Wpf,Linq,Generics,Foreach,在WPF应用程序中,我使用LINQ查询从ObservableCollection获取值并更新某些对象的状态: var shava = from clc in _ShAvaQuCollection where clc.xyID == "x02y02" select clc; switch (shava.First().Status) { case 1: x02y02.Status = MyCustContro
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) 要遍历\u shavaqcollection
中的所有记录,并使用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 coll)
{
foreach(coll中的变量xyid)
{
//执行我的操作(1)和(2)
}
}
请帮助我在foreach循环的最后一段代码中组织这样的迭代。我在理解如何查询循环中集合的每个特定记录时遇到问题
我描述了所有这些,只是为了弄清楚我打算在这个循环中如何处理这样的查询结果 我似乎成功地做到了这一点(而且效果很好):
public void ReadingCollection(System.Collections.Generic.IEnumerable coll)
{
foreach(coll中的变量id)
{
MyCustControl sc=(MyCustControl)this.FindName(id.xyID);
开关(标识状态)
{
案例1:
sc.Status=MyCustControl.Status.First;
打破
案例2:
sc.Status=MyCustControl.Status.Second;
打破
案例3:
sc.Status=MyCustControl.Status.Third;
打破
违约:
打破
}
}
}
我似乎成功地做到了这一点(而且效果很好):
public void ReadingCollection(System.Collections.Generic.IEnumerable coll)
{
foreach(coll中的变量id)
{
MyCustControl sc=(MyCustControl)this.FindName(id.xyID);
开关(标识状态)
{
案例1:
sc.Status=MyCustControl.Status.First;
打破
案例2:
sc.Status=MyCustControl.Status.Second;
打破
案例3:
sc.Status=MyCustControl.Status.Third;
打破
违约:
打破
}
}
}
看起来您需要获取一系列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;
//...
}
}
这看起来正确吗?看起来您需要获取一系列
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.