C# how to use multiple lines instead of 1
本问题已经有最佳答案,请猛点这里访问。
我有一个很长的SQL代码,但为了方便起见,我想把它分成多行阅读。如何在C控制器中执行此操作。以下是我的代码。我知道我们应该加上
1 2 3 4 5 6 7 8 | 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();) |
答:
1 2 3 4 5 6 7 8 9 10 11 | 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(); |
您的意思是要将SQL查询作为字符串放在不同的行中吗?如果是这样,您需要学习用于操作字符串的C语法。
因此,例如,可以实现字符串连接,例如使用
1 2 3 4 5 6 | var name ="Nahuel"; var greetings ="Hello,"; var concatenatedText = greetings + name; // value of concatenatedText equals"Hello, Nahuel" |
这就像我要直接连接字符串值一样:
1 2 3 | var concatenatedText ="Hello," + name; // value of concatenatedText equals"Hello, Nahuel" |
在字符串串联中使用
1 2 3 | var longText ="xxxxxxxxxxx" + "yyyyyyyyyyy" + "zzzzzzzzzzz"; |
也可以使用字符串格式方法:
1 2 3 4 | var name ="Nahuel"; var formattedString = String.Format("Hello, {0}!", name); // value of concatenatedText equals"Hello, Nahuel!" |
也就是说,该格式看到有一个参数(0),并接受与之一起使用的变量来替换它。
您还可以检查StringBuilder类以在某些情况下获得更好的性能。
尝试在字符串开头使用@
1 2 3 4 5 6 7 8 | 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所写,可以将字符串与"+"符号连接起来,然后添加查询参数(Princid、CustID):
协助职能:
1 2 3 4 5 6 7 8 9 10 11 | 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"; } |