Question

can I build a custom query in ormlite at runtime ? for example

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 }

and then I would call the repository with

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);

and leave the actual linq creation be the responsibility of the repository.

if possible, will this affect the performance of ormlite ?

Was it helpful?

Solution

turns out that Select takes an SqlExpression as a parameter. and inside the SqlExpression, the query can be built using Sql Syntax.

example:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top