C#String Concationation问题为什么不是+ =在这里工作?

cod*_*000 1 c# c#-3.0

我有以下c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringTest
{
    class Program
    {
        static void Main(string[] args)
        {

               String strSQLCode;
            strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
                      += " from view_dg_game_details gd (nolock) "
                      += " where gd.gametypeid = {0} " 
                      += " and gd.numberofrounds = {1} "
                      += " and gd.gamevalues = '{2}' ";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我收到一个错误"作业的左侧必须是变量,属性或索引器".

我无法看到错误试图告诉我的内容.我已经注释掉了违规行,但错误只是向上移动了一行.

我可以使用此方法使字符串concation工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringTest
{
    class Program
    {
        static void Main(string[] args)
        {

            String strSQLCode;
            strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * ";
            strSQLCode = strSQLCode +  " from view_dg_game_details gd (nolock) ";
            strSQLCode = strSQLCode + " where gd.gametypeid = {0} ";
            strSQLCode = strSQLCode + " and gd.numberofrounds = {1} ";
            strSQLCode = strSQLCode + " and gd.gamevalues = '{2}' ";            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释这个错误是什么吗?

谢谢

肯

Jus*_*ner 12

因为您不能在不+=重复运行的变量的情况下将运算符串在一起:

strSQLCode = @"select rank() over (order by percentagecorrect desc, 
                                            totalmilliseconds asc) as rank, * ";
strSQLCode += " from view_dg_game_details gd (nolock) ";
strSQLCode += " where gd.gametypeid = {0} ";
strSQLCode += " and gd.numberofrounds = {1} ";
strSQLCode += " and gd.gamevalues = '{2}' ";
Run Code Online (Sandbox Code Playgroud)

如果你想将它声明为"长"单线,请使用 +

strSQLCode = @"select rank() over (order by percentagecorrect desc, 
                                            totalmilliseconds asc) as rank, * "
           + " from view_dg_game_details gd (nolock) "
           + " where gd.gametypeid = {0} " 
           + " and gd.numberofrounds = {1} "
           + " and gd.gamevalues = '{2}' ";
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想要其中任何一个,您可以使用单个字符串文字:

strSQLCode = 
    @"select rank() over (order by percentagecorrect desc, 
                                totalmilliseconds asc) as rank, *
      from view_dg_game_details gd (nolock)
      where gd.gametypeid = {0}
          and gd.numberofrounds = {1}
          and gd.gamevalues = '{2}' ";
Run Code Online (Sandbox Code Playgroud)