Special thanks to Rex M for this bit of wisdom:
public IEnumerable<Friend> FindFriends()
{
//Many thanks to Rex-M for his help with this one.
//http://stackoverflow.com/users/67/rex-m
return doc.Descendants("user").Select(user => new Friend
{
ID = user.Element("id").Value,
Name = user.Element("name").Value,
URL = user.Element("url").Value,
Photo = user.Element("photo").Value
});
}
After finding all of a users friends, I need to show them on a WPF form. I have a problem that not all users have at least 5 friends, some even have no friends! Here's what I have:
private void showUserFriends()
{
if (friendsList.ToList().Count > 0)
{
friend1.Source = new BitmapImage(new Uri(friendsList.ToList()[0].Photo));
label11.Content = friendsList.ToList()[0].Name;
friend2.Source = new BitmapImage(new Uri(friendsList.ToList()[1].Photo));
label12.Content = friendsList.ToList()[1].Name;
//And so on, two more times. I want to show 4 friends on the window.
}
}
So this question has two parts:
How do you suggest I handle the varying number of friends a user might have. With my current code if a user has no friends I get an IndexOutOfBounds exception because friendsList[0] doesn't exist.
How can I more efficiently handle the validation of whether or not a user has friends? Calling .ToList() seems very taxing.