Kentico自定义表查询和/或组合

时间:2017-09-01 02:00:25

标签: kentico

我在Kentico 10中有一个自定义表。有些记录的纬度和经度为零。我试图排除这些行,同时保持现有的搜索郊区,州,邮政编码。 And / Or的当前组合不起作用。有没有一个建议的,更好的方法?

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 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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.