I use the following the C# Linq expression to find a list of processes
with unique names and it returned the results I
expected. However, curiously, the VB version did not seem to work.
Can anyone correct my VB version so that it returns the same results
as the C# version?

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

When I say that the VB version doesn't work, I mean that it returns a
list, but the duplicates are not removed.

Thanks,

Chris

Re: Linq discrepancy between C# and VB.Net by Patrice

Patrice
Thu Mar 13 13:24:26 CDT 2008

In VB you have to use the "key" keyword (don't ask me why ?) :

Dim uniqueProcs = Process.GetProcesses() _
.OrderBy(Function(p) p.ProcessName) _
.Select(Function(p) New With {KEY p.ProcessName}) _
.Distinct()

My personal preference would be to use :

Dim uniqueprocs = From p In Process.GetProcesses _
Order By p.ProcessName _
Select p.ProcessName Distinct


--
Patrice


"Chris Dunaway" <dunawayc@gmail.com> a écrit dans le message de news:
1fd2f365-96c7-4485-8abf-85556051c155@e39g2000hsf.googlegroups.com...
>I use the following the C# Linq expression to find a list of processes
> with unique names and it returned the results I
> expected. However, curiously, the VB version did not seem to work.
> Can anyone correct my VB version so that it returns the same results
> as the C# version?
>
> 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()
>
> When I say that the VB version doesn't work, I mean that it returns a
> list, but the duplicates are not removed.
>
> Thanks,
>
> Chris



Re: Linq discrepancy between C# and VB.Net by Chris

Chris
Thu Mar 13 14:16:40 CDT 2008

On Mar 13, 1:24 pm, "Patrice" <http://www.chez.com/scribe/> wrote:
> In VB you have to use the "key" keyword (don't ask me why ?) :
>
> Dim uniqueProcs = Process.GetProcesses() _
> .OrderBy(Function(p) p.ProcessName) _
> .Select(Function(p) New With {KEY p.ProcessName}) _
> .Distinct()
>
> My personal preference would be to use :
>
> Dim uniqueprocs = From p In Process.GetProcesses _
> Order By p.ProcessName _
> Select p.ProcessName Distinct
>
> --

Yes, so would, but the question originally arose from how to databind
the result to a DataGridView. The query you show won't display
properly in a DataGridView. Instead it has to be changed as so:

Dim uniqueprocs = From p In Process.GetProcesses _
Order By p.ProcessName _
Select New With {Key p.ProcessName}
Distinct

Thanks for the response and the solution.

Chris

Re: Linq discrepancy between C# and VB.Net by Nick

Nick
Fri Mar 14 09:10:45 CDT 2008


"Chris Dunaway" <dunawayc@gmail.com> wrote in message
news:2cb3e8eb-be93-4251-8e1f-6a19c901a8a1@k13g2000hse.googlegroups.com...
> On Mar 13, 1:24 pm, "Patrice" <http://www.chez.com/scribe/> wrote:
>> In VB you have to use the "key" keyword (don't ask me why ?) :
>>
>> Dim uniqueProcs = Process.GetProcesses() _
>> .OrderBy(Function(p) p.ProcessName) _
>> .Select(Function(p) New With {KEY p.ProcessName}) _
>> .Distinct()
>>
>> My personal preference would be to use :
>>
>> Dim uniqueprocs = From p In Process.GetProcesses _
>> Order By p.ProcessName _
>> Select p.ProcessName Distinct
>>
>> --
>
> Yes, so would, but the question originally arose from how to databind
> the result to a DataGridView. The query you show won't display
> properly in a DataGridView. Instead it has to be changed as so:
>
> Dim uniqueprocs = From p In Process.GetProcesses _
> Order By p.ProcessName _
> Select New With {Key p.ProcessName}
> Distinct
>
> Thanks for the response and the solution.
>
> Chris

Paul Vick has an explanation here for the difference in behaviour: -
http://www.panopticoncentral.net/archive/2007/05/11/20566.aspx

Nick Hall