2017-09-01 51 views
0

我在Kentico 10中有一個自定義表格。某些記錄的空白經度和緯度爲空。我試圖排除這些行,同時維持現有的郊區,州,郵政編碼搜索。 And/Or的當前組合不起作用。有沒有建議,更好的方法?Kentico自定義表格查詢和/或組合

var locationsQuery = CustomTableItemProvider.GetItems("customtable.ProjectName_PostcodeSuburb") 
              .WhereNotNull("Latitude") 
              .And() 
              .WhereNotNull("Longitude") 
              .And() 
              .WhereLike("Suburb", locationLike) 
              .Or() 
              .WhereLike("Postcode", locationLike) 
              .Or() 
              .WhereLike("State", locationLike) 
              .Or() 
              .WhereLike("Suburb + ', ' + State + ', ' + Postcode", locationLike) 
              .Columns("Suburb, State, Postcode") 
              .OrderBy("Suburb, State, Postcode") 
              .TopN(20); 

我也嘗試過作爲參數,似乎工作,但擔心SQL注入和不知道如何通過SQL參數。

string whereSql = string.Format("Latitude IS NOT NULL AND Longitude IS NOT NULL AND (Suburb LIKE '{0}' OR Postcode LIKE '{0}' OR State LIKE '{0}')", locationLike); 
var locationsQuery = CustomTableItemProvider.GetItems("customtable.ClearView_PostcodeSuburb", whereSql, "Suburb, State, Postcode", 20, "Suburb, State, Postcode"); 

回答

2

找到的解決方案,在文檔中不太容易找到。需要創建一個WhereCondition並將其作爲查詢中的哪個位置傳遞。

var localityWhereCondition = new WhereCondition().WhereLike("Suburb", locationLike) 
       .Or() 
       .WhereLike("Postcode", locationLike) 
       .Or() 
       .WhereLike("State", locationLike) 
       .Or() 
       .WhereLike("Suburb + ', ' + State + ', ' + Postcode", locationLike); 

var locationsQuery = CustomTableItemProvider.GetItems("customtable.ProjectName_PostcodeSuburb") 
       .WhereNotNull("Latitude") 
       .And() 
       .WhereNotNull("Longitude") 
       .And() 
       .Where(localityWhereCondition) 
       .Columns("Suburb, State, Postcode") 
       .OrderBy("Suburb, State, Postcode") 
       .TopN(20); 
+0

嗨。請問您通過[反饋鏈接](https://docs.kentico.com/k10/custom-development/retrieving-database-data-using-objectquery-api#feedback-link)提交文檔反饋?鏈接可以在每個文檔頁面的底部找到。請告訴我們您在搜索過程中使用了哪些關鍵字,您最終在哪個頁面上找到了解決方案,以及/或者您需要自己弄清楚哪些部分。謝謝,我們非常感謝。 – rocky

0

你可能得到的是如何在這裏訪問它的整個要點 - 在同一頁面https://docs.kentico.com/k10/custom-development/retrieving-database-data-using-objectquery-api

參考快照。

WhereEquals("ColumnName", value) - checks the equality of the value in the specified column with the value specified in the second parameter. 
WhereGreaterThan("ColumnName", value) - compares the value in the specified column with the second parameter. 
WhereNull("ColumnName") - select only rows where the specified column has a NULL value. 
WhereNot(whereCondition) - negates the specified where condition. 
WhereStartsWith("ColumnName", "Value") - only works for text columns. Selects only rows whose value in the specified column starts with the given value.