> var procs = from process in
> System.Diagnostics.Process.GetProcesses()
> select process.ProcessName;
>
> Ok, continuing the discussion, using the query above as a start, what
> is procs after I'm done? Is it an IEnumerable<string>?
Yes, that's an IEnumerable<string>. Hover over "var" to see what type
the compiler has inferred.
> Secondly, how would I select processes with a unique name?
Do you mean select a distinct set of process names?
var uniqueProcs = Process.GetProcesses()
.Select(proc => proc.ProcessName)
.Distinct();
> I tried this:
>
[quoted text clipped - 3 lines]
>
> But that doesn't produce what I thought.
Hmm... I suspect that gave you back a distinct sequence of all the
*characters* which are in any of the processes.
SelectMany takes each element of the original sequence and generates a
subsequence *per element*. The overload you've used just flattens that
resulting subsequence. Now, the delegate you've used to convert an
original element (Process) to a subsequence is to take the process name
- which is a string, i.e. a sequence of characters.
> Finally, why didn't the C# team include all the query operators like
> the VB team did? I hope they will be included in the future.
I hope they won't. I like a simple language with a big library behind
it. The existing query expressions already complicate the language, but
they cover the biggest uses. Calling methods explicitly when they don't
cover it is reasonable to me - but adding yet more complexity to the
language would be a mistake IMO.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Chris Dunaway - 13 Mar 2008 18:54 GMT
> Do you mean select a distinct set of process names?
>
> var uniqueProcs = Process.GetProcesses()
> .Select(proc => proc.ProcessName)
> .Distinct();
Thanks, here's what I ended up with and it returned the results I
expected. However, curiously, the VB version did not seem to work:
C# version works (new keyword used so data binding will work
correctly):
var uniqueProcs = Process.GetProcesses()
.OrderBy(p => p.ProcessName)
.Select(p => new { p.ProcessName })
.Distinct();
VB version doesn't work (New keyword used so data binding will work
correctly):
Dim uniqueProcs = Process.GetProcesses() _
.OrderBy(Function(p) p.ProcessName) _
.Select(Function(p) New With {p.ProcessName}) _
.Distinct()
And when I say it doesn't work, I mean that it returns a list, but the
duplicates are not removed.
> > Finally, why didn't the C# team include all the query operators like
> > the VB team did? I hope they will be included in the future.
[quoted text clipped - 4 lines]
> cover it is reasonable to me - but adding yet more complexity to the
> language would be a mistake IMO.
I agree, but I still think having at least distinct, skip, and take
would be nice.
An expression like this seems a little nicer than using the extension
methods with lambdas, not that the latter is too difficult to read
though.
//Non working code
var procs = from process in Process.GetProcesses()
orderby process.ProcessName
select new { process.ProcessName }
distinct;
Cheers,
Chris
Jon Skeet [C# MVP] - 13 Mar 2008 20:30 GMT
> > Do you mean select a distinct set of process names?
> >
[quoted text clipped - 23 lines]
> And when I say it doesn't work, I mean that it returns a list, but the
> duplicates are not removed.
This is because by default, VB anonymous types are mutable. Change it
to
New With { Key p.Processname }
and I think you'll find it's okay.
Frankly I find this choice a bizarre one on the part of the VB team.
The rest of the world is running towards immutability, and VB decides
to default to mutability...
> > > Finally, why didn't the C# team include all the query operators like
> > > the VB team did? I hope they will be included in the future.
[quoted text clipped - 17 lines]
> select new { process.ProcessName }
> distinct;
I don't think that's much better than:
var procs = (from process in Process.GetProcesses()
orderby process.ProcessName
select new { process.ProcessName })
.Distinct();
The difference is a mere 5 characters - not a lot compared with the
complexity cost to the language. Note that the existing query
expression clauses all save redundancy in terms of expressing a lambda
expression.
I guess we'll have to agree to differ though.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk