清单<> .ForEach未找到 [英] List<> .ForEach not found

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

问题描述

我移植一个Windows Phone应用程序赢得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));



不过,我得到这个错误:

But I get this error:

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进入,的ForEach不可用在Windows应用商店中的应用程序(注意成员后面的小图标)。

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



将变成:

would become:

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天全站免登陆