强类型查询语法 |
Top Previous Next |
NBearLite的基本强类型查询语法主要由QueryColumn,ExpressionClip,WhereClip和OrderByClip这几个强类型查询辅助类组成。
QueryColumnsGenerator.exe工具会为数据库中的表/视图字段自动生成QueryColumn实例,例如 Northwind.Categories.CategoryID,Northwind.Products.ProductName。
QueryColumn从ExpressionClip继承来许多操作符重载和和数据库操作对应的字符串/日期处理函数。QueryColumn和ExpressionClip及自定义value之间可以进行强类型操作符连接的各种运算。
例如:
Northwind.Products.UnitPrice + Northwind.Products.UnitPrice + 100 <- 返回一个ExpressionClip实例 Northwind.Categories.CategoryID > 100 && Northwind.Categories.CategoryName.Length > 3 <- 返回一个WhereClip实例
您可以看到,ExpressionClip和其他ExpressionClip或value间的加减乘除等运算返回的还是ExpressionClip实例,而ExpressionClip和其他ExpressionClip或value由==,!=,>,<,>=,<=等比较操作符连接时,返回WhereClip实例。
WhereClip实例一般用于Update/Delete/Select查询时指定Where()条件。
同时,WhereClip也是继承自ExpressionClip的。也就是说,一个WhereClip可以作为一个ExpressionClip来使用,此时,这个WhereClip表示一个Boolean类型的表达式。
例如:
我们可以将Northwind.Categories.CategoryID > 100 && Northwind.Categories.CategoryName.Length > 3作为一个值,Update给某个表的字段。
QueryColumn类同时包含两个属性Asc和Desc,这两个属性都是OrderByClip类型的实例。OrderByClip实例主要用于Select查询的OrderBy()。多个OrderByClip实例可以使用&操作符连接。
例如:
Northwind.Categories.CategoryName.Desc & Northwind.Categories.CategoryID.Asc <- 表示先按CategoryName降序,再按CategoryID升序排序。
所有的.NET内置的算术操作符都已经重载,可应用于这些强类型查询辅助类。
除了.NET包含的操作符之外,还有很多数据库段的操作符或方法,以ExpressionClip的属性或方法的形式存在。
例如:
//In Northwind.Categories.CategoryID.In(1, 2, 3) <- CategoryID是1或2或3 Northwind.Categories.CategoryID.In(db.Select(Northwind.Products, Northwind.Products.CategoryID).Where(Northwind.Products.UnitPrice > 100).ToSubQuery()) <- CategoryID在一个子查询中,关于db.Select()的使用,请参考Select。
//字符串函数 Northwind.Categories.CategoryName.Contains("test") Northwind.Categories.CategoryName.Like("%test%") Northwind.Categories.CategoryName.StartsWith("test") Northwind.Categories.CategoryName.EndsWith("test") Northwind.Categories.CategoryName.Length Northwind.Categories.CategoryName.ToUpper() Northwind.Categories.CategoryName.ToLower() Northwind.Categories.CategoryName.Trim() Northwind.Categories.CategoryName.SubString(2) Northwind.Categories.CategoryName.SubString(2, 3) Northwind.Categories.CategoryName.IndexOf("test") Northwind.Categories.CategoryName.Replace("test", "ReplaceText")
//日期函数 ExpressionClip.GetCurrentDate() <- 这个比较重要,是代表SQL Server服务器端的当前时间,相当于SQL Server的getdate()。 Northwind.Employees.BirthDate > (Expression.GetCurrentDate() - new TimeSpan(365 * 30, 0, 0)) <- BirthDate > 30年以前,注意,TimeSpan可以和DateTime类型的表达式进行各种运算 Northwind.Employees.BirthDate.GetYear() Northwind.Employees.BirthDate.GetMonth() Northwind.Employees.BirthDate.GetDay()
//聚合查询函数(注意,聚合函数不能用于Where()条件中,只能用于 Select()中,作为查询返回的列) Northwind.Categories.CateforyID.Distinct Northwind.Categories.CateforyID.Count Northwind.Categories.CateforyID.Count(true) <- Count Distinct Northwind.Categories.CateforyID.Count(false) <- Count Not Distinct Northwind.Categories.CateforyID.Sum() Northwind.Categories.CateforyID.Min() Northwind.Categories.CateforyID.Max() Northwind.Categories.CateforyID.Avg()
//二进制操作符
Db1.Table1.IntColumn1.BitwiseAnd(2) Db1.Table1.IntColumn1.BitwiseOr(Db1.Table1.IntColumn2) Db1.Table1.IntColumn1.BitwiseXOr(Db1.Table1.IntColumn3) |