如何从查询 C# 中删除最后一个逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11771741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:10:46  来源:igfitidea点击:

how to remove last comma from query C#

c#asp.net

提问by Rocky

I am doing something as follows..

我正在做如下事情..

querybuilder = ("SELECT Type, Text FROM [Element] WHERE Id IN( ");
foreach (var item in CaseIdList)
{
   querybuilder += item + ",";
}
querybuilder += ")";

at the last insdie the closing bracket I am getting a comma. How to remove that.

在最后一个 insdie 结束括号我得到一个逗号。如何删除它。

采纳答案by Habib

use TrimEnd(',');to remove last comma from the string, string.TrimEnd

用于TrimEnd(',');从字符串中删除最后一个逗号,string.TrimEnd

After the foreach loop use:

在 foreach 循环之后使用:

querbuilder = querybuilder.TrimEnd(',');

Instead of concatenating string, you may use StringBuilderclass.

您可以使用StringBuilder类,而不是连接字符串。

回答by james31rock

querybuilder = ("SELECT Type, Text FROM [Element] WHERE Id IN( ");
foreach (var item in CaseIdList)
{
   querybuilder += item + ",";
}
querbuilder = querybuilder.TrimEnd(',');
querybuilder += ")";

回答by Ria

Try using String.Trim:

尝试使用String.Trim

Removes all leading and trailing white-space characters from the current String object.

从当前 String 对象中删除所有前导和尾随空白字符。

querybuilder = querybuilder.Trim(',');

回答by Tomtom

Instead of the foreach-loop u can use the string.Join method. Take a look at this

您可以使用 string.Join 方法代替 foreach 循环。看看这个

回答by Jaiden

Try to use Replace():

尝试使用Replace()

static void Main()
{
   string temp = "(a, b, c, d,)";
   temp = temp.Replace(",)", ")");

   Console.WriteLine(temp);
   Console.ReadKey();
}

回答by TheEvilPenguin

I would use String.Join:

我会使用 String.Join:

querybuilder = "SELECT Type, Text FROM [Element] WHERE Id IN( " + String.Join(",", CaseIdList.ToArray()) + ")";

I would also look into using Parameters instead of constructing SQL with strong concatenation - string concatenation is vulnerable to SQL injection attacks, and parameters are easy to use.

我还将研究使用参数而不是构造具有强连接的 SQL - 字符串连接容易受到 SQL 注入攻击,并且参数易于使用。

How you would switch to parameters depends on how you're accessing the database, and which database engine, but a quick google search should help.

您将如何切换到参数取决于您访问数据库的方式以及哪个数据库引擎,但快速的谷歌搜索应该会有所帮助。

回答by Pieter Germishuys

You can use String.Join(",", item);This means there is no ugly trimming or splitting that you have to do.

您可以使用String.Join(",", item);这意味着您无需进行丑陋的修剪或拆分。

回答by Jon Skeet

Firstly, I'd try to avoid including the values themselves directly. Use parameters where possible - it's less simple for IN(x, y, z)style parameters, but you can either use a table-valued parameteror simply dynamically create the parameter list (e.g. to IN(@p0, @p1, @p2))

首先,我会尽量避免直接包含值本身。尽可能使用参数 -IN(x, y, z)样式参数不太简单,但您可以使用表值参数或简单地动态创建参数列表(例如 to IN(@p0, @p1, @p2)

Secondly, I'd avoid using string concatenation in a loop - use StringBuilderif you really have to loop round.

其次,我会避免在循环中使用字符串连接 -StringBuilder如果您真的必须循环使用,请使用。

Thirdly, I'd use string.Jointo avoid having to loop at all:

第三,我会string.Join用来避免循环:

string commaSeparated = string.Join(", ", values);

回答by Uriil

You can use LINQ Aggregate and string.Format method:

您可以使用 LINQ Aggregate 和 string.Format 方法:

    var items = CaseIdList.Select(p=>p.ToString(CultureInfo.InvariantCulture)).Aggregate((i,j)=>i+", "+j)
    querybuilder = string.Format("SELECT Type, Text FROM [Element] WHERE Id IN ({0})", items);

回答by user854301

Example using StringBuilder:

使用示例StringBuilder

            var querybuilder  = new StringBuilder("SELECT Type, Text FROM [Element] WHERE Id IN(");
            foreach (var fragment in fragments)
            {
                querybuilder.Append(fragment);
                querybuilder.Append(',');
            }

            // note: we need to delete last line terminator
            querybuilder.Remove(querybuilder.Length - 1, 1);
            querybuilder.Append(')');
            return querybuilder.ToString();