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?

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 assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to set the default page base type to UmbracoViewPage in Umbraco

In this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…

Read Post

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post