Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串列表连接为单个字符串_C#_Json_String_Concatenation - Fatal编程技术网

C# 将字符串列表连接为单个字符串

C# 将字符串列表连接为单个字符串,c#,json,string,concatenation,C#,Json,String,Concatenation,我正在尝试将字符串列表连接成一个由逗号分隔的字符串。使用string.Join非常简单,我面临的问题是如何使用属性实现这一点 public class JsonObject { public string EntityID { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Address3 { get; set;

我正在尝试将字符串列表连接成一个由逗号分隔的字符串。使用
string.Join非常简单,我面临的问题是如何使用属性实现这一点

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 = ??
}
我想将这些类别(1,2,3)连接到单个字符串中,即

public string Category;

首先,我建议不要列出包含非常相似信息的变量列表,而应该将它们分组到单个容器中

public string[] Addresses { get; protected set; }
确保将数组初始化为构造函数中所需的大小。此外,考虑列表是否真的应该是动态的。 如果您想要一个特殊的助手来连接字符串,只需将问题抽象出来即可。基本上,您希望迭代一组字符串

protected IEnumerable<string> GetStringData()
{
  yield return EntityID;
  foreach (var address in Addresses)
    yield return address;
}
编辑如果您想使用问题中所述的
类别
属性:

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

首先,我建议不要列出包含非常相似信息的变量列表,而应该将它们分组到单个容器中

public string[] Addresses { get; protected set; }
确保将数组初始化为构造函数中所需的大小。此外,考虑列表是否真的应该是动态的。 如果您想要一个特殊的助手来连接字符串,只需将问题抽象出来即可。基本上,您希望迭代一组字符串

protected IEnumerable<string> GetStringData()
{
  yield return EntityID;
  foreach (var address in Addresses)
    yield return address;
}
编辑如果您想使用问题中所述的
类别
属性:

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

如果需要分隔符分隔的字符串,则:

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

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

如果需要分隔符分隔的字符串,则:

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

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

我希望我正确地理解了您的问题,因为您希望知道如何通过类属性进行连接

如果是这样,那么我建议的解决方案如下:

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
        }
    }

在类中有一个公共字段或属性,其中包含连接的值,然后在属性设置器中调用类中的私有方法,该方法为您进行连接。

我希望我正确理解您的问题,因为您希望知道如何通过类属性进行连接

如果是这样,那么我建议的解决方案如下:

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
        }
    }


在类中有一个公共字段或属性,其中包含连接的值,然后在属性设置器中调用该类中为您进行连接的私有方法。

String.Join
自.NET 1.1以来就一直存在。我认为OP的意思是“如果您没有使用.NET 4.+”@abatishchev-我不确定这有什么不同。乍一看,这是一种扩展方法。如果不是3.0(我记不清)的话,它们至少在3.5中可用。@Tim:
String.Join
只接受
t[]
,而不是
IEnumerable
@abatishchev-啊,明白了。显然已经过了我的就寝时间。感谢您的澄清:)
String.Join
从.NET 1.1开始就存在了。我想OP的意思是“如果您没有使用.NET 4.+”@abatishchev-我不知道这有什么不同。乍一看,这是一种扩展方法。如果不是3.0(我记不清)的话,它们至少在3.5中可用。@Tim:
String.Join
只接受
t[]
,而不是
IEnumerable
@abatishchev-啊,明白了。显然已经过了我的就寝时间。感谢您的澄清:)不幸的是,我无法控制JSON,所以我必须处理我所得到的东西。我已经更新了这个问题,让它更清楚一点。然后看看@abatishchev answer。不幸的是,我无法控制JSON,所以我必须处理我得到的东西。我已经更新了问题,使其更加清晰。然后看@abatishchev answer我已经编辑了我的问题,以便更清楚地了解我需要什么。地址的当前格式没有问题。@payo:对不起,你是什么意思?没有收到您的评论。@Corey:您想将类别列表“展平”为一个字符串吗?您已经编辑了答案,现在我的回答是僵尸指针……但我刚才说的是那个字符串。连接重载包括string.Join(string,params object[])(至少从.net 4.0开始)@Corey:这不会立即发生。当实体是对象时,C#属性仅在C#中起作用。See和similarI对我的问题进行了编辑,以便更清楚地了解我的要求。地址的当前格式没有问题。@payo:对不起,你是什么意思?没有收到您的评论。@Corey:您想将类别列表“展平”为一个字符串吗?您已经编辑了答案,现在我的回答是僵尸指针……但我刚才说的是那个字符串。连接重载包括string.Join(string,params object[])(至少从.net 4.0开始)@Corey:这不会立即发生。当实体是对象时,C#属性仅在C#中起作用。参见和类似