| SqlExpressionEGetSelectSql Method |
Namespace: LiteRepository
public string GetSelectSql( Type type = null, Expression<Func<E, bool>> where = null, Object param = null, Expression<Func<IEnumerable<E>, IEnumerable<E>>> orderBy = null )
Suppose we have a class:
public class User { [SqlKey] public int Id { get; set; } public string FirstName { get; set; } public string SecondName { get; set; } public DateTime Birthday { get; set; } [SqlIgnore] public string FullName { get { return FirstName + " " + SecondName; } } }
Without any parameters method returns:
var sql = GetSelectSql(); // select Id, FirstName, SecondName, Birthday from User;
If you need to select only parts of columns:
var p = new { FirstName = "", SecondName = "" }; var sql = GetSelectSql(type:p.GetType()); // select FirstName, SecondName from User;
If you need to filter values with hard-coded parameters:
var sql = GetSelectSql(where: e => e.FirstName.StartsWith("A")); // select Id, FirstName, SecondName, Birthday from User where FirstName like 'A%';
If you need to filter values with parameters:
var parameters = { FirstLetter = "A" }; var sql = GetSelectSql(where: e => e.FirstName.StartsWith(parameters.FirstLetter), param: parameters); // select Id, FirstName, SecondName, Birthday from User where FirstName like @FirstLetter%';
If you need sort the results:
var sql = GetSelectSql(l => l.OrderBy(x => x.FirstName).OrderByDescending(x => x.Id)); // select Id, FirstName, SecondName, Birthday from User order by FirstName, Id desc;
You can combine settings to achieve the desired result. If any option will be skipped - SQL will be generated without it.