keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.vb > 03/2008

 #1  
03-13-08, 05:59 PM
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
 #2  
03-13-08, 06:24 PM
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
 #3  
03-13-08, 07:16 PM
Chris Dunaway
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
 #4  
03-14-08, 02:10 PM
Nick Hall
"Chris Dunaway" <dunawayc> wrote in message
news:a8a1
> On Mar 13, 1:24 pm, "Patrice" <http://www.chez.com/scribe/> wrote:
>
> 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/arc.../11/20566.aspx

Nick Hall
Similar Threads
Question about LinQ (LinQ to Sql)

I have already heard that LinQ to Sql will be replaced by LinQ to Entities or something like that. My question is below: that the whole concept of LinQ whatever -- it is...

Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code?

Maybe I shouldn't be using XML for what I want. I want to store some program data and retrieve it easily, iterating through elements grabbing child elements easily and...

Combination of Linq to sql and Linq to xml

Hi folks I am pretty new to linq and in one of my first samples I tried to do the following: In my sample-database I have a table which has some columns and one of them has...

Mixing linq to sql and linq to xml (in linqpad)

Hi group, In my database, I have a table with fields like this : id | title | xml ------------------------------------ 1 | title1 | ...

Linq to SQL - Return DataTable as a result of Linq query

Hi! How to get result od dataTable from Linq query? I have typied DataSet and I want to join couple of tables. And I have a problem with change this result to DataTable...


All times are GMT. The time now is 05:37 AM. | Privacy Policy