列表<>.ForEach 未找到 [英] List<> .ForEach not found

查看:42
本文介绍了列表<>.ForEach 未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am porting a Windows Phone app to Win 8, and I have found this stumbling block, but cant find the solution.

我有一个:

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

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

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

I have added items to my tempItems etc, so then I do this:

  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没有这个功能?我引用了以下内容:

Why could that be, does Win8 not have this function? I am referencing the following:

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 不可用,是否有替代方法可以做到?

If ForEach is not available, is there an alternative that does the same?

推荐答案

根据 MSDN 条目,Windows 商店应用程序中不提供 ForEach(注意成员后面的小图标).

According to the MSDN entry, ForEach is not available in windows store apps (notice the small icons behind the members).

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

That being said, the ForEach method is generally not very much more helpful than simply using a foreach loop. So your code:

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

我认为就表达能力而言,这并不重要.

I would argue that in terms of expressiveness it doesn't matter really.

这篇关于列表&lt;&gt;.ForEach 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆