LINQ Select: different projects same code different results
The same开发者_运维技巧 code on two different web sites (on the same solution), VB.Net (framework 3.5).
The Code:
Public Class UserTest
Public hhh As Integer
Public fff As String
Public Sub New(ByVal hh As Integer, ByVal ff As String)
Me.hhh = hh
Me.fff = ff
End Sub
End Class
Dim lst As List(Of UserTest) = New List(Of UserTest)
lst.Add(New UserTest(1, "x"))
lst.Add(New UserTest(2, "y"))
Dim myData = lst.Select(Function(o) New With {.id = o.fff, .name = o.hhh})
One select returns property’s names with capital letters the other without.
alt text http://img8.imageshack.us/img8/4509/linqbug1.jpg
I tried changing the properties names and no capital letters at all.
Dim myData = lst.Select(Function(o) New With {.prop1 = o.fff, .prop2 = o.hhh})
alt text http://img695.imageshack.us/img695/5231/linqbug2.jpg
Thanks.
In the code sample you posted, the property names in the anonymous object initializer are written with a lowercase first letter. Are you sure that the code in the other website is really the same ? I suspect it uses uppercase first letters :
Dim ggg = StaticData.GetLocationsByText(data, CountryId).Select( _
Function(o) New With { _
.Id = o.UniqueLocation, _
.Text = o.DisplayLocation}).ToList()
精彩评论