C#如何使用多行代替1

时间:2014-11-09 12:04:14

标签: c# string

我有一个很长的SQL代码,但是想要将它分解成多行更容易 读。如何在控制器中执行此操作。以下是我的代码。我知道我们应该这样做 添加&_或其他东西来表示换行符和+或其他东西......不确定 语法..

result = db.Database.SqlQuery<FRT>("SELECT DISTINCT PRODUCTS.[Item Num],    PRODUCTS.Description, PRODUCTS.[Case Pack]
                                                         FROM PRODUCTS INNER JOIN (ORDERS INNER JOIN [ORDER_DETAILS] 
                                                         ON ORDERS.[Order Number] = [ORDER_DETAILS].[Order Number]) 
                                                         ON (PRODUCTS.[Item Num] = [ORDER_DETAILS].[Item Num]) 
                                                         AND (PRODUCTS.[Princ ID] = ORDERS.[Princ ID])
                                                         WHERE ((PRODUCTS.[Princ ID] = '')  AND (ORDERS.[Cust ID] = '' ) 
                                                         AND (PRODUCTS.Discontinued = 'False'))
                                                         ORDER BY PRODUCTS.Description;).ToList();)

回答:

    string sql = @"SELECT DISTINCT PRODUCTS.[Item Num] as [Item_Num], PRODUCTS.Description, PRODUCTS.[Case Pack] as [Case_Pack] 
                                                    FROM PRODUCTS INNER JOIN (ORDERS INNER JOIN [ORDER_DETAILS]
                                                    ON ORDERS.[Order Number] = [ORDER_DETAILS].[Order Number]) 
                                                    ON (PRODUCTS.[Item Num] = [ORDER_DETAILS].[Item Num]) 
                                                    AND (PRODUCTS.[Princ ID] = ORDERS.[Princ ID]) 
                                                    WHERE ((PRODUCTS.[Princ ID] = '" + PrincId + @"')  
                                                    AND (ORDERS.[Cust ID] = '" + CustId + @"' ) 
                                                    AND (PRODUCTS.Discontinued = 'False')) 
                                                    ORDER BY PRODUCTS.Description ";

            result = db.Database.SqlQuery<FRT>(sql).ToList();

2 个答案:

答案 0 :(得分:0)

您的意思是您希望将SQL查询作为字符串放在不同的行中吗?如果是这样,您需要学习用于操作字符串的C#语法。

例如,可以实现字符串连接,例如使用+符号:

var name = "Nahuel";
var greetings = "Hello, ";

var concatenatedText = greetings + name;

// value of concatenatedText equals "Hello, Nahuel"

这就像我直接连接字符串值一样:

var concatenatedText = "Hello, " + name;

// value of concatenatedText equals "Hello, Nahuel"

在字符串连接中使用+符号时,可以使用不同的行,这对于在一行中读取字符串的时间非常有用:

var longText = "xxxxxxxxxxx" +
               "yyyyyyyyyyy" + 
               "zzzzzzzzzzz";

您还可以使用String Format方法:

var name = "Nahuel";
var formattedString = String.Format("Hello, {0}!", name);

// value of concatenatedText equals "Hello, Nahuel!"

这就是,格式看到有一个参数(0)并带有与它一起使用的变量来替换它。

在某些情况下,您还可以查看StringBuilder class以获得更好的效果。

答案 1 :(得分:-2)

尝试在字符串

的开头使用@
result = db.Database.SqlQuery<FRGHT_CO>(@"SELECT DISTINCT PRODUCTS.[Item Num],   PRODUCTS.Description, PRODUCTS.[Case Pack]
                                                     FROM PRODUCTS INNER JOIN (ORDERS INNER JOIN [ORDER_DETAILS] 
                                                     ON ORDERS.[Order Number] = [ORDER_DETAILS].[Order Number]) 
                                                     ON (PRODUCTS.[Item Num] = [ORDER_DETAILS].[Item Num]) 
                                                     AND (PRODUCTS.[Princ ID] = ORDERS.[Princ ID])
                                                     WHERE ((PRODUCTS.[Princ ID] = '')  AND (ORDERS.[Cust ID] = '' ) 
                                                     AND (PRODUCTS.Discontinued = 'False'))
                                                     ORDER BY PRODUCTS.Description;).ToList();)

或者,正如Nahuel Ianni写的那样,你可以将字符串连接到&#39; +&#39;符号,然后添加您的查询参数(PrincID,CustID):

提供辅助功能:

 public String BuildQuery(int princId, int CustId)
        {
            return "SELECT DISTINCT PRODUCTS.[Item Num],   PRODUCTS.Description, PRODUCTS.[Case Pack] " +
                                                     "FROM PRODUCTS INNER JOIN (ORDERS INNER JOIN [ORDER_DETAILS] " +
                                                     "ON ORDERS.[Order Number] = [ORDER_DETAILS].[Order Number]) " +
                                                     "ON (PRODUCTS.[Item Num] = [ORDER_DETAILS].[Item Num]) " +
                                                     "AND (PRODUCTS.[Princ ID] = ORDERS.[Princ ID]) " +
                                                     "WHERE ((PRODUCTS.[Princ ID] = " + princId + ")  AND (ORDERS.[Cust ID] = " + CustId + " ) " +
                                                     "AND (PRODUCTS.Discontinued = 'False')) " +
                                                     "ORDER BY PRODUCTS.Description";
        }