简体   繁体   中英

Put multiple row values in object in VB.NET

I have a stored procedure and often that procedure will return multiple rows, for example:

ID     Type    Price
1234    A      2.260
1234    B      2.690
1234    C      2.990
1234    D      2.690
1234    D      2.790
1234    D      2.650
1234    D      2.680

And I want to output the latest value for each type. In my data reader I have:

While dr.Read
    result.price.TypeA= dr("price")
    result.price.TypeB= dr("price")
    result.price.TypeC= dr("price")
    result.price.TypeD= dr("price")
End While

and my query looks like:

select  sm_id,
        type,
        price
from    ** WITH (NOLOCK)
where   id = @id
order by id desc

I'm not sure how to store all of my results into my object so I can access them in my front end.

This is may be not most performant query but it will do the job as what you described it

select t1.id, t1.t, t1.maxDt, t2.price
from 
    (select id, [TYPE] t, MAX(ud) maxDt 
     from dbo.a it
     where id =1 
     group by id, [type]) t1 
         left join
     dbo.a t2 on t1.id = t2.id and t1.t = t2.[type] and t1.maxDt = t2.ud

Now, storing results in object and displaying it

Public Class MyClass
    Public Property Id As Integer
    Public Property [Type] As String
    Public Property [Date] As DateTime
    Public Property Price As Decimal
End Class

. . . . . 

Dim myList As New List(Of MyClass)()
While dr.Read()
    Dim item As New MyClass()
    item.Id = dr("id")
    item.[Type] = dr("t")
    item.[Date] = dr("maxDt")
    item.Price = dr("price")
    myList.Add(item)
End While

myDataGrid.DataSource = myList

This is what you, approximately, need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM