List<> .ForEach 未找到[重复]

2024-03-22

我正在将 Windows Phone 应用程序移植到 Win 8,我发现了这个绊脚石,但找不到解决方案。

我有一个:

 List<items> tempItems = new List<items>();

and

ObservableCollection<items> chemists = new ObservableCollection<items>();

我已将项目添加到我的 tempItems 等中,所以我这样做:

  tempItems.OrderBy(i => i.Distance)
                .Take(20)
                .ToList()
                .ForEach(z => chemists.Add(z));

但我收到这个错误:

Error   1   'System.Collections.Generic.List<MyApp.items>' does not contain a definition for 'ForEach' and no extension method 'ForEach' accepting a first argument of type 'System.Collections.Generic.List<MyApp.items>' could be found (are you missing a using directive or an assembly reference?) 

为什么Win8没有这个功能呢?我参考以下内容:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;

如果 ForEach 不可用,是否有其他方法可以实现相同的功能?


根据MSDN条目 http://msdn.microsoft.com/en-us/library/windows/apps/6sh2ey19.aspx, ForEach 在 Windows 应用商店应用程序中不可用(请注意成员后面的小图标)。

话虽这么说,ForEach 方法通常并不比简单地使用 foreach 循环更有帮助。所以你的代码:

tempItems.OrderBy(i => i.Distance)
         .Take(20)
         .ToList()
         .ForEach(z => chemists.Add(z));

会成为:

var items = tempItems.OrderBy(i => i.Distance).Take(20);
foreach(var item in items)
{
    chemists.Add(item);
}

我认为就表现力而言,这并不重要。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

List<> .ForEach 未找到[重复] 的相关文章

随机推荐