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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:03:25+00:00 2026-06-07T01:03:25+00:00

In a lot of apps I write, I find myself assigning a default value

  • 0

In a lot of apps I write, I find myself assigning a default value for a subclass of a DBML object. Here is an example of the current way I’m having to do it:

var viewModel = new RandomViewModel
{
    ContactName = (Order.Customer != null ? Order.Customer.ContactName : "Unknown"),
    Date = Order.OrderDate,
    Shipper = (Order.Shipper != null ? Order.Shipper.CompanyName : "Fedex"),
    ShipCity = Order.ShipCity,
    ShipRegion = Order.ShipRegion,
    FirstCategory = (Order.OrderDetails.First().Product.Category != null
        ? Order.OrderDetails.First().Product.Category.CategoryName
        : "DefaultCategory",
    ItemCount = Order.OrderDetails.Count
};

Ideally it would be most readable as this:

var viewModel = new RandomViewModel
{
    ContactName = Order.Customer.ContactName ?? "Unknown",
    Date = Order.OrderDate,
    Shipper = Order.Shipper.CompanyName ?? "Fedex",
    ShipCity = Order.ShipCity,
    ShipRegion = Order.ShipRegion,
    FirstCategory =
        Order.OrderDetails.First().Product.Category.CategoryName
        ?? "DefaultCategory",
    ItemCount = Order.OrderDetails.Count
};

but there is a NullReferenceException for any foreign keyed object that is null. I don’t think null coalescing is usable here, but is there some other consise way to accomplish this very frequent task? Perhaps with a class/method extension, or even a VS macro?

I used the Northwind database for this example:

northwind db

Open to any and all suggestions.

  • 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-07T01:03:27+00:00Added an answer on June 7, 2026 at 1:03 am

    Here’s one possible approach to take. Declare partial class Order, with properties like this:

    string CustomerNameOrDefault { get { return Customer != null ? Customer.ContactName : "Unknown"; } }
    

    Then your code becomes:

    var viewModel = new RandomViewModel
    {
        ContactName = Order.CustomerNameOrDefault,
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find that writing web apps and WinForm apps generally come out a lot
I'm trying to write an Android 2.2 app that will find installed apps that
I'm writing a lot of JavaScript widgets and small apps using the Object Literal
I find that my C# apps do a lot of queries with a lot
A lot of apps pop up a transparent view with rounded corners and an
I know a lot of apps give you some sort of bonus when you
There are a lot of iOS apps in the iTunes Store that, upon launch,
A lot of users on one of our online apps complain about the app
I have a lot of experience writing Delphi 6 Pro apps using the Skype
I've got a lot controller in my Codeigniter apps, ex: Signup, Profile, Main, etc..

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.