Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:45:20+00:00 2026-06-02T02:45:20+00:00

I have a foreach loop that loops trought a list of objects. The meaning

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:45:21+00:00Added an answer on June 2, 2026 at 2:45 am

    You could use LINQ:

    var facebookURL = contacts.Where(c => c.ContactTypeID == 1)
                  .Select(c => c.url)
                  .FirstOrDefault();
    if(facebookURL != null)
        FacebookIcon.NavigateUrl = "http://facebook.com/" + facebookURL;
    

    Edit: Actually you could benefit of LINQ’s deferred execution to reuse the same for every type of contact-type:

    var contactType = 1; // facebook
    var url = contacts.Where(c => c.ContactTypeID == contactType)
          .Select(c => c.url);
    if (url.Any())
        FacebookIcon.NavigateUrl = "http://facebook.com/" + url.First();
    contactType = 2;    // google
    if (url.Any())
        GoogleIcon.NavigateUrl = "http://Google.com/" + url.First();
    

    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):

    var urlTypeMapping = contacts.GroupBy(c => c.ContactTypeID)
        .ToDictionary(grp => grp.Key, grp => grp.Select(c => c.url));
    foreach (var type in urlTypeMapping)
    {
        var typeUrl = type.Value.FirstOrDefault();
        if (typeUrl != null)
        {
            switch (type.Key)
            {
                case 1:
                    FacebookIcon.NavigateUrl = "http://facebook.com/" + typeUrl;
                    break;
                case 2:
                    GoogleIcon.NavigateUrl = "http://Google.com/" + typeUrl;
                    break;
                default:
                    break; //or throw new Exception("Invalid type!");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of objects and I'd like to loop over that list
in a ASP.NET application (MVC) I have a foreach loop that loops through a
I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
In PHP I can use a foreach loop such that I have access to
I have a datatable that I add to the chart in foreach loop as
I have a loop that can look like this: For Each article In artAll
hey guys, we have a loop that: 1.Loops over several thousand xml files. Altogether
I have a page that includes a list of lists and I'd like to
I have a class that looks like this: public class MyModel{ public int TheId
Yay I finally thought of a title! I have a foreach loop that iterates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.