C # to vb.net convsersion

I am trying to convert a C # block to vb.

I used the service on developerfusion.com to convert, but when I paste it into Visual Studio, it agrees with the "Key" operations ("The field name or property initialized in the object initializer must begin with '.'").

I played with the code for several hours trying to get around this, but everything I did led to big mistakes.

So, I began to wonder if the conversion was right in developerfusion.

Here is C # for vb.net.

I'm not sure where the Key comes from, and wondered if anyone could enlighten me.

Thanks!

FROM

var combinedResults = cars.Select(c=>new carTruckCombo{ID=c.ID,make=c.make,model=c.model}) .Union(tracks.Select(t=>new carTruckCombo{ID=t.ID,make=t.make,model=t.model})); 

For

 Dim combinedResults = cars.[Select](Function(c) New carTruckCombo() With { _ Key .ID = c.ID, _ Key .make = c.make, _ Key .model = c.model _ }).Union(tracks.[Select](Function(t) New carTruckCombo() With { _ Key .ID = t.ID, _ Key .make = t.make, _ Key .model = t.model _ })) 
+6
source share
2 answers

Remove Key

do it instead:

  Dim combinedResults = cars.Select(Function(c) New carTruckCombo() With { _ .ID = c.ID, _ .make = c.make, _ .model = c.model _ }).Union(tracks.Select(Function(t) New carTruckCombo() With { _ .ID = t.ID, _ .make = t.make, _ .model = t.model _ })) 

As a side note, this converter always worked better for me whenever I needed it:

http://converter.telerik.com/

+7
source

In C #, when creating an anonymous type, it generates an Equals and GetHashCode implementation for you, using all the properties of your anonymous type.

VB.NET does something similar, but requires that you put the Key modifier in the properties of your anonymous type.

C # β€œjust does it”, where VB.NET gives you the flexibility to determine which properties are used in equality. Since C # uses all the properties, the converter gives you Key in everything, so equality works the same.

OK, so what's the background of the Key modifier, so what happened to your conversion?

The converter seems to have incorrectly assumed that you are using an anonymous type, but it is not. Your type is carTruckCombo , so Key modifiers do not work. Removing the Key modifier will fix the problem, since you have a well-defined class where you can implement your equality there.

+5
source

All Articles