简体   繁体   English

从C#中的结构构建逗号分隔的字符串

[英]Build comma seperated string from the struct in C#

I have the following struct in C# class 我在C#类中具有以下结构

public struct Employee
        {
            public const string EMPID = "EMP_ID";
            public const string FName = "FIRST_NAME";
            public const string LNAME = "LAST_NAME";
            public const string DEPTID = "DEPT_ID";

        }

Is there an easy way to build a string as follows 有没有一种简单的方法来构建字符串,如下所示

const string mainquery="INSERT INTO EMP(EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID) VALUES(:EMP_ID,:FIRST_NAME,:LAST_NAME,:DEPT_ID)"

Instead of doing as follows and then concatenating it. 而不是执行以下操作然后将其串联。

const string EMP_COLS=
                EMPLOYEE.EMPID + "," +
                EMPLOYEE.FNAME + "," +
        EMPLOYEE.LNAME + "," +
                EMPLOYEE.DEPTID;

            const string EMP_Values=
                EMPLOYEE.EMPID + ":" +
                EMPLOYEE.FNAME + ":" +
        EMPLOYEE.LNAME + ":" +
                EMPLOYEE.DEPTID;

You could try something like this: 您可以尝试这样的事情:

StringBuilder sb = new StringBuilder();
var fields = typeof(Employee).GetFields();
for (int i = 0; i < fields.Length; ++i)
{
    sb.Append(fields[i].GetValue(new Employee()));
    if (i < fields.Length - 1)
    {
        sb.Append(',');
    }
}

string result = sb.ToString();
// The above will be "EMP_ID,FIRST_NAME,LAST_NAME,DEPT_ID"

Edit : Note that above I'm assuming you've got using directives for both System.Reflection and System.Text . 编辑 :请注意,上面我假设您已经对System.ReflectionSystem.Text using指令。

If the struct held readonly properties rather than public consts you could do it with Reflection. 如果该结构具有只读属性而不是公共const,则可以使用Reflection进行操作。 You could call Type.GetProperties , loop through them all and call them to get out the values and insert the values into a List<string> and then join them with string.Join(", ", myList); 您可以调用Type.GetProperties ,遍历它们并调用它们以获取值,然后将这些值插入List<string> ,然后将它们与string.Join(", ", myList);连接起来string.Join(", ", myList); or something. 或者其他的东西。
You'd then get the first part of the string as EMP_ID, FIRST_NAME, LAST_NAME, DEPT_ID and you'd need to make a copy of that and add the : s as needed and you'd have both parts. 然后,您将获得字符串的第一部分,即EMP_ID, FIRST_NAME, LAST_NAME, DEPT_ID并且需要对其进行复制,并根据需要添加: ,并且您将拥有这两个部分。

you can use the string.Format(string format, object[] arg) Method as follows: 您可以使用string.Format(string format, object[] arg)方法,如下所示:

string query = string.Format("INSERT INTO EMP({0},{1},{2},{3}) VALUES(:{0},:{1},:{2},:{3})",
                Employee.EMPID,
                Employee.FNAME,
                Employee.LNAME,
                Employee.DEPTID);

Hope that helps. 希望能有所帮助。

您将需要某种映射字典,例如here ,或者您可以使用某种ORM工具为您完成映射。

        string[] values = new[] {
            "EMP_ID",
            "FIRST_NAME",
            "LAST_NAME",
            "DEPT_ID",
        };

        var columns = string.Join(",", values);
        var parameters = ":" + string.Join(",:", values);

        var sql = string.Format("INSERT INTO EMP({0}) VALUES({1})",
            columns,
            parameters);
string[] cols = typeof(Employee).GetFields()
                 .Select(f => f.GetValue(null).ToString()).ToArray();

Console.WriteLine(string.Format(
      "INSERT INTO EMP({0}) VALUES(:{1})", 
      string.Join(",", cols), 
      string.Join(",:", cols)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM