我可以在运行时在Ormlite中构建自定义查询吗?

时间:2013-12-20 04:15:48

标签: c# linq servicestack ormlite-servicestack

我可以在运行时在ormlite中构建自定义查询吗? 例如

public class SearchCriteria
{
    public string FieldName { get; set; }
    public MatchType MatchType { get; set; }
    public object value1 { get; set; }
    public object value2 { get; set; }
}
public enum MatchType { StartsWith, Contains, Between, GreaterThan } // ... etc.
public class OrderCriteria
{
    public string FieldName { get; set; }
    public OrderDirection OrderDirection { get; set; }
}
public enum OrderDirection { Ascending, Descending }

然后我会用

调用存储库
SearchCriteria[] sCrit = new SearchCriteria[2];
sCriteria[0] = new SearchCriteria{ FieldName = "Description", MatchType = MatchType.Contains, value1 = "vip" }
sCriteria[1] = new SearchCriteria{ FieldName = "Description", MatchType = MatchType.Contains, value1 = "client" }
OrderCriteria[] oCrit = new OrderCriteria[] { new OrderCriteria{ FieldName = "Description", OrderDirection = OrderDirection.Ascending} };
repo.GetItemsList(sCriteria, oCrit);

并保留实际的linq创建是存储库的责任。

如果可能,这会影响ormlite的性能吗?

1 个答案:

答案 0 :(得分:3)

结果是Select将SqlExpression作为参数。 在SqlExpression中,可以使用Sql语法构建查询。

示例:

SqlExpression<T> expression = new MySqlExpression<T>();
                expression.WhereExpression = (whereExp.Length > 0 ? "WHERE " + whereExp : "");
                expression.OrderByExpression = (orderExp.Length > 0 ? "ORDER BY " + orderExp : "");
                expression = expression.Limit(skip: _pageIndex * _pageSize, rows: _pageSize);