Is there an error in this code from 101 LINQ samples on MSDN? (Update: fixed)

NOTE: Charlie Calvert replied below that 101 LINQ Samples has now been updated with the correct code.

Visual C # MSDN Developer Center has 101 LINQ samples . I found this through a Bing search.

SelectMany Code - Connection of 1 :

public void Linq14() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var pairs = from a in numbersA, b in numbersB where a < b select new {a, b}; Console.WriteLine("Pairs where a < b:"); foreach (var pair in pairs) { Console.WriteLine("{0} is less than {1}", pair.a, pair.b); } } 

However, this code will not compile . I noticed that if I remove the comma at the end from a in numbersA, and instead add from before b in numbersB , it will compile and work fine:

  var pairs = from a in numbersA from b in numbersB where a < b select new {a, b}; 

I am not sure if this is an error in the MSDN example or maybe I am running a C # and .NET version that does not support this syntax.

If I look at the palette at the top of page 101 of LINQ Samples , I see that it says โ€œFuture Versionsโ€. Does this mean that future versions of C # /. NET will support using a comma instead of from in LINQ syntax?

LINQ_sample_breadcrumb.png

I am using Visual Studio 2008 Standard with .NET 3.5 SP1.

+4
source share
3 answers

Yes, we just updated most of 101 samples with new code, which should be less scary for these problems. We published a lot of new code, and there are still some glitches, especially around the interval, but hopefully we are in better shape than we are. Try accessing the link now and see if it looks better:

http://msdn.microsoft.com/en-us/vcsharp/aa336758.aspx

  • Charlie
+2
source

Yes, this is a mistake in the sample.

I strongly suspect that this was a pre-release version where this syntax may have been supported. I expect it to still appear under "Future Versions" because at the time it was written, it was about a future version.

This syntax, of course, is not supported in C # 4, which is the only future version that has been universally recognized.

+4
source

This is the mistake John was talking about. In addition, samples incorrectly display non-existent methods: Fold and EqualAll . They have been replaced by Aggregate and SequenceEqual respectively.

0
source

All Articles