Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active June 30, 2021 07:30
Show Gist options
  • Save gistlyn/0d37f496391721eab100514cbf4808c4 to your computer and use it in GitHub Desktop.
Save gistlyn/0d37f496391721eab100514cbf4808c4 to your computer and use it in GitHub Desktop.
example-queries
public class Author
{
public Author(){}
[AutoIncrement]
[Alias("AuthorID")]
public Int32 Id { get; set;}
[Index(Unique = true)]
[StringLength(40)]
public string Name { get; set;}
public DateTime Birthday { get; set;}
public DateTime ? LastActivity { get; set;}
public Decimal? Earnings { get; set;}
public bool Active { get; set; }
[StringLength(80)]
[Alias("JobCity")]
public string City { get; set;}
[StringLength(80)]
[Alias("Comment")]
public string Comments { get; set;}
public Int16 Rate { get; set;}
}
var dbFactory = HostContext.Resolve<IDbConnectionFactory>();
using var db = dbFactory.OpenDbConnection();
int year = DateTime.Today.AddYears(-20).Year;
db.Select<Author>(x => x.Birthday >= new DateTime(year,1,1)
&& x.Birthday <= new DateTime(year,12,31));
db.Select<Author>(x => Sql.In(x.City, "London","Madrid","Rome"))
db.Select<Author>(x => x.Earnings <= 50);
db.Select<Author>(x => x.Name.StartsWith("A"));
db.Select<Author>(x => x.Name.EndsWith("garzon"));
db.Select<Author>(x => x.Name.Contains("Benedict"));
//implicit Server SQL string casting
db.Select<Author>(x => x.Rate.ToString() == "10");
//Server SQL string concatenation
db.Select<Author>(x => "Rate " + x.Rate == "Rate 10");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment