I have a foreach loop that loops trought a list of objects. The meaning of it is to set a NavigateUrl to a Hyperlink. My code looks like this:
foreach (var con in contacts)
{
if (con.ContactTypeID == 1)
{
FacebookIcon.NavigateUrl = "http://facebook.com/" + con.ContactURL;
}
}
I wonder if their is some better way to do it. I will have about 10 other ContactTypeID and I rather don’t write nine more if else..
You could use LINQ:
Edit: Actually you could benefit of LINQ’s deferred execution to reuse the same for every type of contact-type:
Edit 2: Here’s another approach using a Dictionary mapping all types with their URLs which should be more efficient in case you have millions of types 😉 (@MAfifi):