查
根据id
var result = db.SingleById<Poco>(1);
根据字段
var customer = db.Single<Customer>(new { customer.Email });
动态结果
db.Select<List<object>>(db.From<Poco>().Select("COUNT(*), MIN(Id), MAX(Id)"))[0];
// List<object>:[ 10, 1, 10]
同上
db.Select<Dictionary<string,object>>(db.From<Poco>().Select("COUNT(*) Total, MIN(Id) MinId, MAX(Id) MaxId"))[0]
单行
db.Single<List<object>>(db.From<Poco>().Select("COUNT(*) Total, MIN(Id) MinId, MAX(Id) MaxId"))
硬来 首行首列
object result = db.Scalar<object>(db.From<Poco>().Select(x => x.Id));
in操作 Order表的CustomerId等于Customer表记录(Country等于USA)的id
sql:select * from Order where CustomerId in ( select id from Customer where Country = "USA" )
var usaCustomerIds = db.From<Customer>(c => c.Country == "USA").Select(c => c.Id);
var usaCustomerOrders = db.Select(db.From<Order>()
.Where(q => Sql.In(q.CustomerId, usaCustomerIds)));
db.Select<Author>(q => q.Birthday >= new DateTime(agesAgo, 1, 1)
&& q.Birthday <= new DateTime(agesAgo, 12, 31));
In
db.Select
sql: WHERE "JobCity" In ('London', 'Madrid', 'Berlin')
like a开头
db.Select
WHERE upper("Name") like 'A%'
like GARZON结尾
db.Select(q => q.Name.EndsWith("garzon"));
WHERE upper("Name") like '%GARZON'
WHERE upper("Name") like '%GARZON'
like 包含
db.Select
WHERE upper("Name") like '%BENEDICT%'
and
db.Select(q => q.Rate == 10 && q.City == "Mexico");
WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))
WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))
最大
int maxAgeUnder50 = db.Scalar<Person, int>(x => Sql.Max(x.Age), x => x.Age < 50);
SELECT Max("Age") FROM "Person" WHERE ("Age" < 50)
count 汇总
int peopleOver40 = db.Scalar
int peopleUnder50 = db.Count(x => x.Age < 50);
不通过id Single会加top 1
Person personByAge = db.Single(x => x.Age == 42);
是否存在
bool has42YearOlds = db.Exists(new { Age = 42 });
返回固定字段
List
SELECT "LastName" FROM "Person" WHERE ("Age" = 27)
---
对结果去重
HashSet<int> results = db.ColumnDistinct<int>(db.From<Person>().Select(x => x.Age) .Where(q => q.Age < 50));
SELECT "Age" FROM "Person" WHERE ("Age" < 50)
字典结果
Dictionary<int,string> results = db.Dictionary<int, string>( db.From<Person>().Select(x => new { x.Id, x.LastName }).Where(x => x.Age < 50));
SELECT "Id","LastName" FROM "Person" WHERE ("Age" < 50)
复杂的字典
Dictionary<int, List
SELECT "Age","LastName" FROM "Person" WHERE ("Age" < 50)
SelectFmt 参数格式化
var tracks = db.SelectFmt<Track>("Artist = {0} AND Album = {1}", "Nirvana", "Heart Shaped Box");
等同于
var tracks = db.SelectFmt<Track>("SELECT * FROM track WHERE Artist={0} AND Album={1}","Nirvana", "Heart Shaped Box");
多条记录
List<Track> tracks = db.Select<Track>()
单条记录
Track track = db.Single
字典 前2列
Dictionary<int, string> trackIdNamesMap = db.Dictionary<int, string>( "select Id, Name from Track")
Lookup 字典 前2列 返回的是Dictionary < K, List < V > >
Dictionary<int, List
返回首列
List
HashSet首列值去重
HashSet
Scalar返回单行单列
var trackCount = db.Scalar
默认为and条件
var track3 = db.Where
这种方式被用烂了大街 属性对应参数名
var track3 = db.Select
通过id 返回强类型
var track = db.SingleById
延迟加载 SelectLazy这种以Lazy结尾
var lazyQuery = db.SelectLazy
// Iterate over a lazy sequence
foreach (var person in lazyQuery) {
//...
}
其他类同 如下
var topVIPs = db.WhereLazy
var topVIPs = db.SelectLazyFmt