我有一个以ObservableCollection
为参数的方法。我使用Lambda表达式来填充另一个列表,如下所示:
private void DownloadSources(ObservableCollection<DownloadSourcesHelper> SourcesDownloadCollection)
{
downloadsList =
SourcesDownloadCollection.Select(GetDownloadCommandParameters)
.Select(sourceParamters =>
new DownloadCalculation(DownloadSources.Item currently being iterated here!,
))).ToList();
foreach (var source in downloadsList )
{
blah blah.
// .....
我有办法获得当前的DownloadSources
项吗?
答案 0 :(得分:1)
问题要么像
一样简单var list =...Select(o => new DownloadCalculation(o.Item)).ToList(); // current item is o
或者您可以在选择
中创建匿名类型var list = ....Select(o => new { Item = o, Result = new DownloadCalculation(o).ToList() });
foreach (var source in list )
{
// use source.Item and source.Result
}
也许这是你的问题(将值从一个Select
传递到另一个{/ p>):
var list = ....Select(o => new {Current = o, Result = GetDownloadCommandParameters }.Select(o => new DownloadCalculation(Current, Result)).ToList()
答案 1 :(得分:0)
我猜你在做MVVM,这段代码是在ViewModel中吗?
如果是这样,只需将DownloadSources.SelectedItem绑定到ViewModel中的属性,以便可以在linq查询中使用它。