Linq discrepancy between C# and VB.Net

C

Chris Dunaway

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
 
P

Patrice

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
 
C

Chris Dunaway

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
 
N

Nick Hall

Chris Dunaway said:
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
 

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

Similar Threads

C# LINQ query problem (case insensitive) 2
I need a little help here. 4
Linq 1
linq and gridview sorting 2
Linq and OrderBy 1
LINQ in C# 10
dynamic search and sort using linq 1
LINQ sentence error 1

Top