2

I am trying to concatenate a list of strings into a single string separated by a comma. Pretty straightforward using string.Join, the problem I am facing is how can I do this using a property?

public class JsonObject
{
    public string EntityID { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Address3 { get; set; }

    public List<string> Category = ??
}

I am trying to get a json object and insert it into a DB. Category is an array which I can handle with List<string>. How do I concat this list of strings into a single string and then return it to string Category? I assume you would have to use a separate class to handle it but other than that I am not sure how else to go about it.

The Json Object looks like this:

"EntityID":"foo",
"Categories": [ "Category1", "Category2", "Category3"] 

It is these Categories(1,2,3) that I want to concatenate into the single string i.e.

public string Category;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Corey
  • 1,733
  • 4
  • 18
  • 26

4 Answers4

9

If you need a separator-separated string then:

public List<string> Categories { get; set; }

public string Category
{
    get
    {
        return String.Join(",", Categories);
    }
}

If just concatenate i.e. join by empty string:

public string Category
{
    get
    {
        return String.Concat(Categories);
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I have edited my question to be clearer as to what I require. Addresses are okay in their current format. – Corey Mar 14 '14 at 05:37
  • @payo: Sorry, what do you mean? Didn't get your comment. – abatishchev Mar 14 '14 at 05:42
  • @Corey: Do you want to 'flatten' a list of categories into one string? – abatishchev Mar 14 '14 at 05:44
  • You've edited your answer and now my response is a zombie pointer...But I was saying that string.Join overloads include string.Join(string, params object[]) (at least since .net 4.0) – payo Mar 14 '14 at 05:46
  • I think what you have done above is what I am trying to do. I'll give it a shot and see if it produces the result I am after. – Corey Mar 14 '14 at 05:50
  • Okay so I have tried the above but it isn't working is there any reason why this won't work with Entity Framework Code first? – Corey Mar 18 '14 at 02:26
  • @Corey: Does it happen inside entity's member? And doesn't work - what does it mean? What happens, what's expected, what doesn't happen? – abatishchev Mar 18 '14 at 02:34
  • Yes. Its not creating the column within the database. What I was expecting to happen is that 'Category' will be converted into a string type column but 'Categories' wouldn't as it is not a supported type by EF. Is there a better way to achieve this? – Corey Mar 18 '14 at 02:45
  • 1
    @Corey: This won't happen immediately. C# property will work only in C# while the entity is an object. See http://stackoverflow.com/questions/15585330/entity-framework-calculated-column-codefirst and similar – abatishchev Mar 18 '14 at 02:48
0
public static string Concat(this IEnumerable<string> yourString) {
    StringBuilder category= new StringBuilder();
    foreach(string s in yourString) {
        category.Append(s);
    }
    return category.ToString();
}

if you are using .net 4.0

  String.Join(String.Empty, yourString.ToArray());
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • `String.Join` has been around since .NET 1.1. – Tim Mar 14 '14 at 05:28
  • I think OP means "if you're *not* using .NET 4.+" – abatishchev Mar 14 '14 at 05:30
  • @abatishchev - Not sure I see why that makes any difference. At a glance it's an extension method. Those were available in at least 3.5, if not 3.0 (can't remember exactly). – Tim Mar 14 '14 at 05:33
  • @Tim: `String.Join` [prior 4.0](http://msdn.microsoft.com/en-us/library/dd992421%28v=vs.100%29.aspx) was accepting `T[]` only, not `IEnumerable`. – abatishchev Mar 14 '14 at 05:34
  • @abatishchev - Ah, got it. Past my bedtime, obviously. Thanks for the clarification :) – Tim Mar 14 '14 at 05:35
0

First of all, I would recommend not making a list of variables that hold very similar information, rather you should group them into a single container.

public string[] Addresses { get; protected set; }

Make sure to initialize the array to the size you want in a constructor. Also, consider List if this really should be dynamic in size.

If you want a special helper to join your strings just abstract the problem away. Basically, you want to iterate over a group of strings.

protected IEnumerable<string> GetStringData()
{
  yield return EntityID;
  foreach (var address in Addresses)
    yield return address;
}

Then join the string together using string.Join

string commaDelim = string.Join(",", GetStringData())

EDIT In case you want to use the Category property as you've stated in your question:

public string Category { get { return string.Join(",", GetStringData()); } }
payo
  • 4,501
  • 1
  • 24
  • 32
  • I have no control over the JSON unfortunately so I have to work with what I have been given. I have updated the question to make it a little clearer. – Corey Mar 14 '14 at 05:35
  • Then look at @abatishchev answer – payo Mar 14 '14 at 05:37
0

I hope i am understanding your question correctly, in that you wish to know how to do the concatenation through the class properties.

If so, then here is my suggested solution:

public class JsonObject
    {
        public string CompiledString { get; set; }

        private string _Category;
        public string Category 
        { 
            get
            { 
                return _Category; 
            }
            set
            {
                _Category = value;
                CompileString();
            }
        }

        private string _EntityID;
        public string EntityID
        {
            get
            {
                return _EntityID;
            }
            set
            {
                _EntityID = value;
                CompileString();
            }
        }
        //Rest of the properties go here

        private void CompileString()
        {
            //cycle through each of your properties and update the CompiledString variable
            CompiledString =
                _Category == null ? string.Empty : _Category + "," +
                _EntityID == null ? string.Empty : _EntityID + ",";
            //I left the last comma in there because you will be adding other props... just remember to exclude it from the last one.

            //Of course this part of the implementation is entirely up to you, your question was about how to do it through the property
        }
    }

You have a public field or property in your class which contains the concatenated value and then in your property setters you call a private method in the class which does the concatenation for you.

Stark
  • 444
  • 5
  • 15