3

Windows Phone アプリを Win 8 に移植していますが、この障害を見つけましたが、解決策が見つかりません。

私は持っています:

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

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 が利用できない場合、同じことを行う代替手段はありますか?

4

1 に答える 1

15

MSDN エントリによると、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);
}

表現力という点では、それほど重要ではないと私は主張します。

于 2013-03-16T12:36:28.950 に答える