linq query with compoun from clause wont compile

R

Rich P

The following sample linq query wont compile - 1st error says "a query
body must end with a select clause or a group clause" and then says I
need ";"..., invalid "in". Do I need to add an additional using
statement? Or change the syntax below for a compound from?

------------------------------------------------

The sample uses a compound from clause to compose a query that returns a
sequence of the pairs.

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

Thanks



Rich
 
J

Jeroen Mostert

Rich said:
The following sample linq query wont compile - 1st error says "a query
body must end with a select clause or a group clause" and then says I
need ";"..., invalid "in". Do I need to add an additional using
statement? Or change the syntax below for a compound from?
This particular sample and most others that demonstrate joins have been out
of date for a long time.
var pairs =
from a in numbersA,
b in numbersB
where a < b
select new {a, b};
In LINQ as it was finalized this should be written as:

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

Peter Duniho

The following sample linq query wont compile - 1st error says "a query
body must end with a select clause or a group clause" and then says I
need ";"..., invalid "in". Do I need to add an additional using
statement? Or change the syntax below for a compound from?

As Jeroen says, the syntax is wrong (just as the compiler suggests).

You should take _anything_ you read at a non-official source with a grain
of salt (including this message). If ever you have problems, your first
stop should always be the official source:
http://msdn.microsoft.com/en-us/library/bb397676.aspx "LINQ Query
Expressions (C# Programming Guide)"
http://msdn.microsoft.com/en-us/library/bb310804.aspx "Query Keywords (C#
Reference)"

Pete
 
R

Rich P

var pairs =
In LINQ as it was finalized this should be written as:

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

Beautiful! Worked perfectly. Thanks very much. This is the learning
experience I am looking to achieve with these linq samples. I would
never have figured this out!

Rich
 
J

Jeroen Mostert

Peter said:
As Jeroen says, the syntax is wrong (just as the compiler suggests).
LINQ expressions have a very specific form, so syntax violations can be
easily detected but not as easily corrected. From what the compiler is
complaining about you'd have no idea how to fix it.
You should take _anything_ you read at a non-official source with a
grain of salt (including this message).

Good advice. Unfortunately, the sample he quoted is straight from Microsoft:
http://msdn.microsoft.com/vcsharp/aa336758#SelectManyCompoundfrom1

Now, how official that should be considered to the innocent developer is
arguable, but...

These samples were based on a beta version of LINQ and unfortunately never
updated. Most samples are still usable as-is, but the "SelectMany" examples
(and possibly others as well) should basically be ignored.
If ever you have problems, your
first stop should always be the official source:
http://msdn.microsoft.com/en-us/library/bb397676.aspx "LINQ Query
Expressions (C# Programming Guide)"

The "101 Samples" page should probably be taken down and redirect people to
this page instead.
 
P

Peter Duniho

[...]
You should take _anything_ you read at a non-official source with a
grain of salt (including this message).

Good advice. Unfortunately, the sample he quoted is straight from
Microsoft:
http://msdn.microsoft.com/vcsharp/aa336758#SelectManyCompoundfrom1

Now, how official that should be considered to the innocent developer is
arguable, but...

Well, it's under the "Future Versions" section of the web site. I agree
there might be a good argument for taking the page down altogether, but no
one should ever prefer a pre-release "sample code" web page over the
_actual_ documentation.

Pete
 
J

Jeroen Mostert

Peter said:
[...]
You should take _anything_ you read at a non-official source with a
grain of salt (including this message).

Good advice. Unfortunately, the sample he quoted is straight from
Microsoft:
http://msdn.microsoft.com/vcsharp/aa336758#SelectManyCompoundfrom1

Now, how official that should be considered to the innocent developer
is arguable, but...

Well, it's under the "Future Versions" section of the web site.

FWIW, this depends on your language. I have found no rhyme or reason, but if
MSDN detects you should be served a language other than English, the
breadcrumb navigation that points out where you are may be missing entirely
(as was the case for me). Try switching to "Deutschland - Deutsch" for example.
I agree there might be a good argument for taking the page down
altogether, but no one should ever prefer a pre-release "sample code" web
page over the _actual_ documentation.
My point was that unless you're particularly diligent, you won't find out
that it's pre-release code. "Future versions" is literally the only hint,
two words at the top of the page (assuming they're even there). There's no
"This is prerelease documentation and subject to change" warning like we're
used to from the MSDN. And guess what page comes up as #1 on Google for
"linq sample"? (Thank goodness the *second* hit is more relevant.)

In fact, I'm now convinced this page is a menace to the world! I demand
action be taken immediately before more innocent developers get hurt! To the
Microsoft employees who are undoubtedly hanging on my every word: you have
your work cut out for you.
 
P

Peter Duniho

[...]
My point was that unless you're particularly diligent, you won't find
out that it's pre-release code.

And my point is that there is in fact product documentation, which should
be the first place anyone ever looks when they run into a problem, no
matter what the source of the problem.

Even if the sample code had been marked "Extra-special, super-recent, this
is exactly the code you need to write today", it's still just sample code.

Now, if someone's doing things exactly as the .NET documentation says they
should and they still run into problems (it does happen), then one might
look around for sample code that suggests a work-around, or seek help in a
public forum. But otherwise, the docs are always the first place to look.
[...]
In fact, I'm now convinced this page is a menace to the world!

Uh, okay. I suppose it's true, the world is filled with people to whom
it's never occurred to read the actual documentation.
I demand action be taken immediately before more innocent developers get
hurt! To the Microsoft employees who are undoubtedly hanging on my every
word: you have your work cut out for you.

Did you submit a comment on the web site? I'd be surprised if there are
any Microsoft employees reading this.

Pete
 
J

Jeroen Mostert

Peter said:
[...]
My point was that unless you're particularly diligent, you won't find
out that it's pre-release code.

And my point is that there is in fact product documentation, which
should be the first place anyone ever looks when they run into a
problem, no matter what the source of the problem.
Then you and I would be mostly out of an (admittedly unpaid) job, and
where's the fun in that?

Your point is well-taken, though. Next time, I'm going straight for the C#
Language Specification. That's the *real* documentation, folks. Accept no
substitutes, not even from Microsoft. (The "from" clause is described in
section 7.15.2.4, for the curious.)
[...]
In fact, I'm now convinced this page is a menace to the world!

Uh, okay. I suppose it's true, the world is filled with people to whom
it's never occurred to read the actual documentation.
Absolutely, but I'm more concerned about the people who find a page that's
on top of the most obvious Google queries, from Microsoft itself, have
problems compiling it and then have the gall not to download the MSDN to
page through that instead.

I mean, for sure they're just another class of moron, but I suspect they
might be even more common than the people who just will not read the manual
under any circumstances.
Did you submit a comment on the web site? I'd be surprised if there are
any Microsoft employees reading this.
Yes, I did. And I'm pretty sure there aren't any, my tongue was firmly
planted in my cheek.

Not holding my breath for my comment to be processed, though. I mean, it
might be the official interface, but such things still give no guarantee...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top