开发者

Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)

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

The MSDN Visual C# Developer Center has a section called 101 LINQ Samples. I found this through a Bing search.

The code for SelectMany - Compound from 1 is:

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 won't compile. I noticed that if I remove the comma at the end of from a in numbersA, and instead add from in front of b in numbersB, it will compile and work fine:

        var pairs =
            from a in numbersA
            from b in nu开发者_StackOverflow社区mbersB
            where a < b
            select new {a, b};

I'm not sure if this is a bug in MSDN's example or if possibly I'm running a version of C# and .NET that doesn't support this syntax.

If I look at the breadcrumb at the top of the 101 LINQ Samples website, I see it says "Future Versions". Does this indicate that future versions of C#/.NET will support using a comma instead of from in LINQ syntax?

Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)

I'm using Visual Studio 2008 Standard with .NET 3.5 SP1.


Yes, that's a bug in the sample.

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

This syntax certainly isn't supported in C# 4, which is the only future version which has been publicly acknowledged.


Yes, we just updated most of the 101 Samples with new code that should be less plagued by these kinds of problems. We posted a lot of new code, and there are still some glitches, particularly around spacing, but hopefully we are in better shape than we were. Try accessing the link now, and see if it looks better:

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

  • Charlie


It's a bug as Jon mentioned. In addition, the samples incorrectly list nonexistent methods: Fold and EqualAll. They were replaced by Aggregate and SequenceEqual, respectively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜