To get just the object rather than the entire list, use
FirstOrDefault.
i.e.
Dim C as Car  = (From c In crs Where c.Color.StartsWith("R") Select
c).FirstOrDefault()

instead of

otrocarros.Cars = (From c In crs Where c.Color.StartsWith("R") Select
c).ToList()

Check the result to make sure you found a match, I.e
Dim C as Car  = (From c In crs Where c.Color.StartsWith("R") Select
c).FirstOrDefault()
If C is Nothing then
   ' Handle this case
Else
   ' Process normally
End If

-- R.B. Davidson

On Jun 11, 12:01 pm, "Juan M. Oviedo" <jmo...@hotmail.com> wrote:
> Well, I have some code that uses Framerwork 2.0, and at the time we did not 
> have LINQ capabilities, so now that I can use LINQ and since my collectons 
> are like this (inherit List(of Class)) I was wondering if I could make it 
> work without touching my preexisting classes...
> Your sugestion worked though, but I dont want to add a new property instead 
> of using the object itself.
>
> Juan M. Oviedo
>
> Date: Thu, 11 Jun 2009 18:30:19 +0300
> Subject: [DotNetDevelopment] Re: LINQ Error
> From: sahinceylan2...@gmail.com
> To: DotNetDevelopment@googlegroups.com
>
> Do Cars class have to inherit List(of Car) ?
> If not use this class
>
> Partial Public Class _Default
>     Inherits System.Web.UI.Page
>
>     Protected Sub Page_Load(ByVal sender As Object, ByVal e As 
> System.EventArgs) Handles Me.Load
>
>          Dim crs As New List(Of Car)
>         crs.Add(New Car() With {.Color = "Rojo"})
>         crs.Add(New Car() With {.Color = "Verde"})
>         crs.Add(New Car() With {.Color = "Rojo"})
>
>         crs.Add(New Car() With {.Color = "Azul"})
>
>         Dim otrocarros As New Cars
>         otrocarros.Cars = (From c In crs Where c.Color.StartsWith("R") Select 
> c).ToList()
>         otrocarros.OwnerName = "Juan"
>
>         For Each oc In otrocarros.Cars
>             Response.Write(oc.Color.ToString)
>         Next
>     End Sub
>
> End Class
>
> Public Class Cars
>     Inherits List(Of Car)
>     Private _OwnerName As String
>
>     Public Property OwnerName() As String
>         Get
>             Return _OwnerName
>
>         End Get
>         Set(ByVal value As String)
>             _OwnerName = value
>
>         End Set
>     End Property
>
> End Class
>
> Public Class Cars
>         Private _OwnerName As String
>         Public Property OwnerName() As String
>             Get
>                 Return _OwnerName
>
>             End Get
>             Set(ByVal value As String)
>
>                 _OwnerName = value
>             End Set
>         End Property
>
>         Private _cars As List(Of Car)
>         Public Property Cars() As List(Of Car)
>             Get
>                 Return _cars
>
>             End Get
>             Set(ByVal value As List(Of Car))
>                 _cars = value
>             End Set
>         End Property
>
>     End Class

Reply via email to