【问题标题】:Kentico custom table query and / or combinationKentico 自定义表查询和/或组合
【发布时间】:2017-09-01 02:00:24
【问题描述】:

我在 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");

【问题讨论】:

    标签: kentico


    【解决方案1】:

    找到解决方案,在文档中不太容易找到。需要创建 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);
    

    【讨论】:

    • 嗨。请问您可以通过feedback link 提交对文档的反馈吗?该链接可以在每个文档页面的底部找到。请告诉我们您在搜索过程中使用了哪些关键字,您最终在哪个页面上找到了解决方案,和/或您必须自己弄清楚的具体部分。谢谢,我们真的很感激。
    【解决方案2】:

    您可以在此处了解如何访问它的全部要点 - 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.
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多