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 6951113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:41+00:00 2026-05-27T14:10:41+00:00

I get a list of all users in my database with the following code:

  • 0

I get a list of all users in my database with the following code:

IEnumerable<Firma> firmaer = db.Firma.ToList();

I try to map each user to a ViewModel that looks like this:

public class UserViewModel
{
    public String CVR_Nummer { get; set; }
    public DateTime LastActivityDate { get; set; }
    public DateTime CreationDate { get; set; }
    public String FirmaNavn { get; set; }

}

When the field firma_navn in the datbase is empty, i get a NullReferenceException

model.FirmaNavn = firmaer.Where(x => x.CVR_nummer == user.UserName).FirstOrDefault().firma_navn ?? "Missing";

It throws a NullReferenceException.

I can catch this exception, but i have heard somewhere that catching exceptions is kind of heavy.

What i would like to do is make

model.FirmaNavn = "Missing";

If the field is empty in the database.

Is this possible without catching the exception, or am i wrong about catching exception is heavy?

EDIT

This is how the method looks like now (after help):

public List<UserViewModel> MapUserViewModel(MembershipUserCollection users)
        {
            List<UserViewModel> userviewmodel = new List<UserViewModel>();

            IEnumerable<Firma> firmaer = db.Firma.ToList();

            foreach (MembershipUser user in users)
            {
                UserViewModel model = new UserViewModel();
                model.CVR_Nummer = user.UserName;
                model.CreationDate = user.CreationDate;
                model.LastActivityDate = user.LastActivityDate;

                var firma = firmaer.Where(x => x.CVR_nummer == user.UserName).FirstOrDefault();

                if (firma != null)
                {
if(string.IsNullOrEmpty(firma.firma_navn))
                        model.FirmaNavn = "Missing";
else
model.FirmaNavn = firma.firma_navn;
                }

                /*if (firmaer != null)
                {
                    var firma = firmaer.Where(x => x.CVR_nummer == user.UserName).FirstOrDefault();
                    model.FirmaNavn = firma.firma_navn ?? "Mangler";   
                }
                */
                userviewmodel.Add(model);
            }
            return userviewmodel;
        }

Now works. Thanks.

  • 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-05-27T14:10:42+00:00Added an answer on May 27, 2026 at 2:10 pm

    You are not wrong about catching exceptions being “heavy” – and they should really only be used for the truly exceptional circumstances.

    If firma_navn is likely to be null then it’s not an exceptional circumstance, so you should code accordingly:

    if (firmaer != null)
    {
        var model = firmaer.Where(x => x.CVR_nummer == user.UserName).FirstOrDefault();
        if (model != null)
        {
            model.FirmaNavn = model.firma_navn ?? "Missing";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get a list of all the tables defined for the database
I would like to get the list of users from the database, but I
i want to get list of all application or Threads attached with a process.For
How can I to get list of all wcf services running on a machine?
Looking to get a list of all the files (commit comments would be nice
How do I get a list of all files (and directories) in a given
How do I get a list of all the headings in a word document
How do I get a list of all currently loaded assemblies? Closed as a
How do you get a list of all the tables and use that list
I wish to get a list of all strings that are used in a

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.