2013-12-20 63 views
1

我可以在運行時在ormlite中構建自定義查詢嗎? 例如我可以在運行時在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的性能嗎?

回答

2

事實證明,選擇需要的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); 
相關問題