how to find the max id from a generic list
i m getting a collection of data in lis开发者_如何学Ct's object. And from this list i want to get the maximum id.`
Dim objinfo As List(Of AlbumInfo) = objPhotos.GetPhotos_Alb_ID(Me.ModuleId, hdd_AlbID.Value)
Dim Photo_Image As String = ""
Dim str As String = Photo_Image & fu_Photo.PostedFile.FileName.Substring(fu_Photo.PostedFile.FileName.LastIndexOf("."))
If objinfo.Count >= 1 Then
Photo_Image = Convert.ToString(hdd_AlbID.Value) + "_" + Convert.ToString(objinfo.Item("0").Photo_Id + 1)
Else
Photo_Image = Convert.ToString(hdd_AlbID.Value) + "_" + Convert.ToString("1")
End If
this returns the "0"th positions id from Convert.ToString(objinfo.Item("0").Photo_Id + 1)
but i want to get the last item's id.
Well, you specify max in the title, and last in the question, so here's both:
Max:
Photo_Image = hdd_AlbID.Value.ToString() & "_" & _
objinfo.Max(Function(o) o.Photo_Id) _
.Photo_Id.ToString()
Last:
Photo_Image = hdd_AlbID.Value.ToString() & "_" & _
objinfo.Last().Photo_Id.ToString()
Use Max extension method. For example,
...
maxPhotoId = objInfo.Max(Function(photo) photo.Photo_Id).Photo_Id
...
精彩评论