Goodnight. How to correctly implement a field check for null. I understand the vknet library and want to get a list of photos from the album with the best resolution. When requested, there comes a response with the available photo sizes. But each photo has its maximum size. How to sort incoming data correctly?

public static List<string> GetPhotoList(long ownerID, long albumID) { var photolist = new List<string>(); var photos = vk.Photo.Get(new PhotoGetParams { OwnerId = ownerID, AlbumId = PhotoAlbumType.Id(albumID) }); foreach (var photo in photos) { if (photo.Photo2560 != null) photolist.Add(photo.Photo2560.ToString()); if (photo.Photo1280 != null) photolist.Add(photo.Photo1280.ToString()); } return photolist; } 

I now have such a "bike" but it does not work as it should, it puts a bunch of duplicate links in a heap. If the photo has a field Photo2560 and Photo1280, then in my list both links are entered. I would like to get separate photos in the most affordable resolution that a particular photo has.

    2 answers 2

    I understand that ToString () does not return the desired result and you need to take values ​​from specific fields. Then this option will do:

     public static List<string> GetPhotoList(long ownerID, long albumID) { var photos = vk.Photo.Get(new PhotoGetParams { OwnerId = ownerID, AlbumId = PhotoAlbumType.Id(albumID) }); var photolist = photos.Where(x => x.Photo2560 != null || x.Photo1280 != null) .Select(x=> x.Photo2560?.ToString() ?? x.Photo1280.ToString()) .ToList(); return photolist; } 
    • ToString to overtake from Uri in string. - Petr

    LINQ

     var highPhotoSize = in photo from photos where photo.Photo2560 != null || photo.Photo1280 != null select photo.Photo2560 != null ? photo.Photo2560.ToString() : photo.Photo1280.ToString(); 

    The result will be an IEnumerable<string> with all links.