根据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(q => Sql.In(q.City, "London", "Madrid", "Berlin"));
sql: WHERE "JobCity" In ('London', 'Madrid', 'Berlin')
like a开头
db.Select(q => q.Name.StartsWith("A"));
WHERE upper("Name") like 'A%'
---
like GARZON结尾
db.Select(q => q.Name.EndsWith("garzon"));
WHERE upper("Name") like '%GARZON'
---
like 包含
db.Select(q => q.Name.Contains("Benedict"));
WHERE upper("Name") like '%BENEDICT%'
and
db.Select(q => q.Rate == 10 && q.City == "Mexico");
WHERE (("Rate" = 10) AND ("JobCity" = 'Mexico'))
---
最大
int maxAgeUnder50 = db.Scalar(x => Sql.Max(x.Age), x => x.Age < 50);
SELECT Max("Age") FROM "Person" WHERE ("Age" < 50)
count 汇总
int peopleOver40 = db.Scalar( db.From().Select(Sql.Count("*")).Where(q => q.Age > 40));
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 results = db.Column(db.From().Select(x => x.LastName).Where(q => q.Age == 27));
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> results = db.Lookup(db.From().Select(x => new { x.Age, x.LastName }).Where(q => q.Age < 50));
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(q => q.RefId == refId)
字典 前2列
Dictionary trackIdNamesMap = db.Dictionary( "select Id, Name from Track")
Lookup 字典 前2列 返回的是Dictionary < K, List < V > >
Dictionary<int, List> albumTrackNames = db.Lookup( "select AlbumId, Name from Track")
返回首列
List trackNames = db.Column("select Name from Track")
HashSet首列值去重
HashSet uniqueTrackNames = db.ColumnDistinct("select Name from Track")
Scalar返回单行单列
var trackCount = db.Scalar("select count(*) from Track")
默认为and条件
var track3 = db.Where(new { AlbumName = "Throwing Copper", TrackNo = 3 })
这种方式被用烂了大街 属性对应参数名
var track3 = db.Select(
"select * from Track Where AlbumName = @album and TrackNo = @trackNo", new { album = "Throwing Copper", trackNo = 3 })
通过id 返回强类型
var track = db.SingleById(1); id等于1的一条
var tracks = db.SelectByIds(new[]{ 1,2,3 }); id等于 1 2 3的三条
延迟加载 SelectLazy这种以Lazy结尾
var lazyQuery = db.SelectLazy("Age > @age", new { age = 40 });
// Iterate over a lazy sequence
foreach (var person in lazyQuery) {
//...
}
其他类同 如下
var topVIPs = db.WhereLazy(new { Age = 27 }).Where(p => IsVip(p)).Take(5)
var topVIPs = db.SelectLazyFmt("Age > {0}", 40).Where(p => IsVip(p)).Take(5)