如何在结果字符串数据中添加字符串

时间:2014-05-02 03:54:12

标签: c# string

我有一个db值

    ╔══════════╦════════════╦═════════════╗
    ║ SatuanID ║  Address   ║ ParentAddr  ║
    ╠══════════╬════════════╬═════════════╣
    ║  100     ║     1      ║      0      ║
    ║  101     ║     2      ║      1      ║
    ║  201     ║     4      ║      2      ║
    ║  102     ║     5      ║      1      ║
    ║  202     ║     6      ║      2      ║
    ║  203     ║     7      ║      5      ║
    ╚══════════╩════════════╩═════════════╝

这是我将数据加载到字符串

的代码
public string child_list, myStr, ipList, childAdd, sub_child, sprtChild;

string sqlChild = "";
sqlChild = string.Format("select Address ,IpSlave , ( Select COUNT (*) From SatuanKawan where ParentAddr = 1) AS Count From SatuanKawan where ParentAddr = 1");
//         sql = string.Format("select * from SatuanKawan");
DataTable dtChild = CGlobalVar.dbaseBMSOps.GetDataTable(sqlChild);
List<string> list = new List<string>();
List<string> listIp = new List<string>();
List<string> listSubChild = new List<string>();
foreach (DataRow rowChild in dtChild.Rows)
{
    count = (string)rowChild["Count"].ToString();
    childAdd = (string)rowChild["Address"].ToString();
    list.Add(rowChild["Address"].ToString());
    listIp.Add(rowChild["IpSlave"].ToString());
    string sqlSubChild = "";
    sqlSubChild = string.Format("select Address From SatuanKawan where ParentAddr = {0}", childAdd);
    DataTable dtSubChild = CGlobalVar.dbaseBMSOps.GetDataTable(sqlSubChild);
    foreach (DataRow rowSubChild in dtSubChild.Rows)
    {
        listSubChild.Add(rowSubChild["Address"].ToString());
    }

}

sub_child = string.Join(",", listSubChild.ToArray());

现在,childAdd值为 2 5

和子级值 4,6,7

我再次使用foreach因为我希望结果子级值为 + 4,6_ + 7 _

string&#34; +&#34;意思是:

查询"select Address From SatuanKawan where ParentAddr = {0}", childAdd中的

childAdd值为 2 5

因此值 4,6 7

string&#34;,&#34;表示在childAdd值之间分开

所以&#34; +&#34; 意味着+ 4,6_ + 7 _

字符串&#34; _&#34; 在4,6和7之间分开

因此值结果为 + 4,6_ + 7 _

如何将字符串subChild结果设为 + 4,6_ + 7 _

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的问题,但在我看来,你需要做的就是用“ +”替换sub_child字符串中的“,”的最后一个实例,然后添加一个字符串开头的“+”和结尾的“”。

换句话说,在发布的代码末尾添加以下代码:

int index = sub_child.LastIndexOf(",");
sub_child = String.Format("+{0}_", sub_child.Remove(index, 1).Insert(index, "_+));

答案 1 :(得分:0)

这对我有用:

var sql = "SELECT Address, IpSlave, ParentAddr FROM SatuanKawan;";
using (var dt = CGlobalVar.dbaseBMSOps.GetDataTable(sql))
{
    var data =
        dt.Rows
            .Cast<DataRow>()
            .Select(dr => new
            {
                Address = dr["Address"].ToString(),
                IpSlave = dr["IpSlave"].ToString(),
                ParentAddr = dr["ParentAddr"].ToString(),
            })
            .ToArray();

    var lookUp = data.ToLookup(x => x.ParentAddr);

    sub_child =
        String.Join("",
            lookUp["1"]
                .Select(x => String.Format("+{0}_",
                    String.Join(",",
                        lookUp[x.Address]
                            .Select(y => y.Address)))));
}

我从您的数据中得到的结果是:

+4,6_+7_