具有复杂SQL的LinqDataSource

时间:2013-07-25 08:25:12

标签: linq linq-to-sql telerik

我有一个相当长的SQL语句组合来给我一个我要在Telerik Ticker中使用的3行结果。据我所见,Telerik Ticker只接受Linq或Entity数据源。

所以我有一个代码为DataSourceID = "LinqDataSource1",其中有一个OnSelecting事件。

我的代码背后有......

Protected Sub LinqDataSource1_Selecting(sender As Object, e As LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting 

Dim db As New TestDataContext 

Dim tickerquery As New StringBuilder 

tickerquery.Append("DECLARE @ALL_Total int ") 
tickerquery.Append("DECLARE @ABC_Total int ") 
tickerquery.Append("DECLARE @XYZ_Total int ") 
tickerquery.Append("DECLARE @ALL_TypeA int ") 
tickerquery.Append("DECLARE @ABC_TypeA int ") 
tickerquery.Append("DECLARE @XYZ_TypeA int ") 
tickerquery.Append("DECLARE @ALL_TypeB int ") 
tickerquery.Append("DECLARE @ABC_TypeB int ") 
tickerquery.Append("DECLARE @XYZ_TypeB int ") 
tickerquery.Append("DECLARE @ALL_TypeC int ") 
tickerquery.Append("DECLARE @ABC_TypeC int ") 
tickerquery.Append("DECLARE @XYZ_TypeC int ") 
tickerquery.Append("DECLARE @ALL_TypeD int ") 
tickerquery.Append("DECLARE @ABC_TypeD int ") 
tickerquery.Append("DECLARE @XYZ_TypeD int ") 
tickerquery.Append("DECLARE @ALL_TypeE int ") 
tickerquery.Append("DECLARE @ABC_TypeE int ") 
tickerquery.Append("DECLARE @XYZ_TypeE int ") 

tickerquery.Append("DECLARE @ALL_Title nvarchar(3) ") 
tickerquery.Append("DECLARE @ABC_Title nvarchar(3) ") 
tickerquery.Append("DECLARE @XYZ_Title nvarchar(3) ") 
tickerquery.Append("SET @ALL_Title = 'ALL' ") 
tickerquery.Append("SET @ABC_Title = 'ABC' ") 
tickerquery.Append("SET @XYZ_Title = 'XYZ' ") 
tickerquery.Append("DECLARE @Count_Tbl table (Title nvarchar(3), Total int, TypeA int, TypeB int, TypeC int, TypeD int, TypeE int) ") 

tickerquery.Append("SELECT @ALL_Total = (SELECT Count(ProdID) FROM Products) ") 
tickerquery.Append("SELECT @ABC_Total = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC') ") 
tickerquery.Append("SELECT @XYZ_Total = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ') ") 
tickerquery.Append("SELECT @ALL_TypeA = (SELECT Count(ProdID) FROM Products WHERE Productstatus = 'TypeA') ") 
tickerquery.Append("SELECT @ABC_TypeA = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC' AND Productstatus = 'TypeA') ") 
tickerquery.Append("SELECT @XYZ_TypeA = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ' AND Productstatus = 'TypeA') ") 
tickerquery.Append("SELECT @ALL_TypeB = (SELECT Count(ProdID) FROM Products WHERE Productstatus = 'TypeB') ") 
tickerquery.Append("SELECT @ABC_TypeB = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC' AND Productstatus = 'TypeB') ") 
tickerquery.Append("SELECT @XYZ_TypeB = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ' AND Productstatus = 'TypeB') ") 
tickerquery.Append("SELECT @ALL_TypeC = (SELECT Count(ProdID) FROM Products WHERE Productstatus = 'TypeC') ") 
tickerquery.Append("SELECT @ABC_TypeC = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC' AND Productstatus = 'TypeC') ") 
tickerquery.Append("SELECT @XYZ_TypeC = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ' AND Productstatus = 'TypeC') ") 
tickerquery.Append("SELECT @ALL_TypeD = (SELECT Count(ProdID) FROM Products WHERE Productstatus = 'TypeD') ") 
tickerquery.Append("SELECT @ABC_TypeD = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC' AND Productstatus = 'TypeD') ") 
tickerquery.Append("SELECT @XYZ_TypeD = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ' AND Productstatus = 'TypeD') ") 
tickerquery.Append("SELECT @ALL_TypeE = (SELECT Count(ProdID) FROM Products WHERE Productstatus <> 'TypeD') ") 
tickerquery.Append("SELECT @ABC_TypeE = (SELECT Count(ProdID) FROM Products WHERE Category = 'ABC' AND Productstatus = 'TypeE') ") 
tickerquery.Append("SELECT @XYZ_TypeE = (SELECT Count(ProdID) FROM Products WHERE Category = 'XYZ' AND Productstatus = 'TypeE') ") 

tickerquery.Append("INSERT INTO @Count_Tbl (Title, Total, TypeA, TypeB, TypeC, TypeD, TypeE SELECT @ALL_Title, @ALL_Total, @ALL_TypeA, @ALL_TypeB, @ALL_TypeC, @ALL_Decomm, @ALL_Running ") 

tickerquery.Append("INSERT INTO @Count_Tbl (Title, Total, TypeA, TypeB, TypeC, TypeD, TypeE SELECT @ABC_Title, @ABC_Total, @ABC_TypeA, @ABC_TypeB, @ABC_TypeC, @ABC_Decomm, @ABC_Running ") 

tickerquery.Append("INSERT INTO @Count_Tbl (Title, Total, TypeA, TypeB, TypeC, TypeD, TypeE SELECT @XYZ_Title, @XYZ_Total, @XYZ_TypeA, @XYZ_TypeB, @XYZ_TypeC, @XYZ_Decomm, @XYZ_Running ") 

tickerquery.Append("SELECT Title, Total, TypeA, TypeB, TypeC, TypeD, TypeE FROM @Count_Tbl") 

Dim results As IEnumerable(Of ProductCount) = _ 
    db.ExecuteQuery(Of ProductCount) _ 
    (tickerquery.ToString) 

e.Result = results 
End Sub

我得到的错误是

Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near the keyword 'SELECT'. at the tickerquery.ToString line

是否有另一种获取结果的方法,以便它们可以绑定到Ticker?

1 个答案:

答案 0 :(得分:0)

修复...在我转换为字符串构建器的一系列附加内容时,我删除了3个插入语句中的一些右括号。

我有

tickerquery.Append(“INSERT INTO @Count_Tbl(Title,Total,TypeA,TypeB,TypeC,TypeD,TypeE SELECT @ABC_Title,@ ABC_otal,@ ABC_TypeA,@ ABC_TypeB,@ ABC_TypeC,@ ABC_Decomm,@ ABC_Running”)< / p>

我应该有

tickerquery.Append(“INSERT INTO @Count_Tbl(Title,Total,TypeA,TypeB,TypeC,TypeD,TypeE)SELECT @ABC_Title,@ ABC_otal,@ ABC_TypeA,@ ABC_TypeB,@ ABC_TypeC,@ ABC_Decomm,@ ABC_Running”)