This post gives you a simple way of getting an address on one line, separated by commas and it ignores any blank lines.

public class Address
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string PostCode { get; set; }
    public string Country { get; set; }
public string OnOneLine => string.Join(", ", ((new List() { Line1, Line2, City, County, PostCode, Country })).Where(x => !string.IsNullOrEmpty(x))); }

So now you can test it like this.

Address test1 = new Address() { Line1 = "10 Street Name", Line2 = "Smallville", City = "Big City", County = "Shire", PostCode = "WC1 1DN", Country = "United Kingdom" };
Console.WriteLine(test1.OnOneLine);
//returns: 10 Street Name, Smallville, Big City, Shire, WC1 1DN, United Kingdom

Address test2 = new Address() { Line1 = "10 Street Name", City = "Big City", PostCode = "WC1 1DN", Country = "United Kingdom" };
Console.WriteLine(test2.OnOneLine);
//returns: 10 Street Name, Big City, WC1 1DN, United Kingdom

Address test3 = new Address() { Line1 = "10 Street Name", Line2 = "Smallville", PostCode = "WC1 1DN" };
Console.WriteLine(test3.OnOneLine);
//returns: 10 Street Name, Smallville, WC1 1DN

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post

How to use Donut Caching in Umbraco and MVC

In this post I show you how easy Donut Caching is to use in Umbraco and MVC. It's very clever and no…

Read Post

How to start working with Umbraco v8

In this post I give you step by step instructions for getting Umbraco v8 forked, checked out and wor…

Read Post

How to add default values to Umbraco properties in the backoffice

This post shows you how you can easily add a default value to a property when using the editor in th…

Read Post