3

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

I have a:

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

and

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

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

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;

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

Dan Sewell
  • 1,278
  • 4
  • 18
  • 45
  • If not already, check that the target framework for the project is actually .net 4.5 and not client profile or something similar. I doubt ForEach is not available in Win8, but this is just a guess hence why it's in a comment. – Dmitriy Khaykin Mar 16 '13 at 12:23
  • What's wrong with a plain foreach loop? That will solve your problem in both environments. – Erik van Brakel Mar 16 '13 at 12:23
  • @ErikvanBrakel while you make a valid point, it seems if ForEach() is indeed available and the OP may have a config or settings issue somewhere, that he would want to resolve it. – Dmitriy Khaykin Mar 16 '13 at 12:25
  • @DavidKhaykin That's why I didn't make it into an answer and put it in the comments ;-) But you're right, there's probably something off somewhere... – Erik van Brakel Mar 16 '13 at 12:27
  • 3
    Wow. It is actually gone in Metro style apps according to this Question and Accepted Answer - http://stackoverflow.com/questions/10299458/is-the-listt-foreach-method-gone – Dmitriy Khaykin Mar 16 '13 at 12:31
  • According to [this msdn link](http://msdn.microsoft.com/en-gb/library/bwabdf9z(v=vs.110).aspx) `ForEach()` is available for Windows 8 only on `NET Framework 4.5`. Check out the `Platforms` at the bottom. If you already have `NET Framework 4.5` on your machine, then make sure you reference the correct DLL because you may have referenced a previous versions.. – Kaf Mar 16 '13 at 12:34
  • So the operative keyword is "Metro style apps" - I haven't delved into Win8 development yet so this is interesting. – Dmitriy Khaykin Mar 16 '13 at 12:35
  • @DavidKhaykin or 'Windows Store apps' which is what they are now called – Dan Sewell Mar 16 '13 at 12:37
  • Actually, the term they're using on MSDN is 'Windows Store App'. Not sure how far that'll get you on Goog... I mean Bing though. – Erik van Brakel Mar 16 '13 at 12:37
  • I always thought that this method should not exist at all. The only advantage is that it reduces keystrokes. Also unlike `foreach` it creates `delegate` that is odd and reduced keystrokes does not worth it, IMO. – Leri Mar 16 '13 at 12:38
  • Yeah, and I've seen a lot of code where people convert an IEnumerablr to List just to use .ForEach() ... – Dmitriy Khaykin Mar 16 '13 at 12:39
  • @DanSewell - I am behind the times, it seems :) – Dmitriy Khaykin Mar 16 '13 at 12:40
  • To add insult to injury, Decimal.Round() is also gone, but thats another matter! – Dan Sewell Mar 16 '13 at 12:42

1 Answers1

15

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

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.

Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66