Select

Top  Previous  Next

Select方法支持大多数SQL的Select常用查询语法,包括:聚合查询,OrderBy,Where,GroupBy,InnerJoin,Paging,SubQuery等。

 

Select方法的格式:

 

其中,[...]表示可选项,{AA|BB}表示或者关系。Table,QueryColumn,ExpressionClip,OrderByClip和WhereClip的使用语法请参见:强类型查询语法

 

//db is an instance of Database

db.Select(Table[, ExpressionClipList])[.Where(WhereClip)][.Join(Table[, AliasName][, OnWhereClip])][.GroupBy(QueryColumnList)][.OrderBy(OrderByClips)].{ToScalar()|ToDataSet()|ToDataReader()|ToDbCommand()|ToSubQuery()|}

 

下面分别举例:

 

简单查询,返回 * (所有字段),不指定任何条件

 

DataSet ds = db.Select(Northwind.Categories).ToDataSet();

 

带Where查询,只返回CategoryName

 

ds = db.Select(Northwind.Categories, Northwind.Categories.CategoryName).Where(Northwind.Categories.CategoryID > 0).ToDataSet();

 

带GroupBy、OrderBy的分页查询,返回CategoryName和Count(CategoryID),按CategoryName分组,按CateoryName降序排序,返回第3-4条记录。

 

ds = db.Select(Northwind.Categories, Northwind.Categories.CategoryName, Northwind.Categories.CategoryID.Count()).GroupBy(Northwind.Categories.CategoryName).OrderBy(Northwind.Categories.CategoryName.Desc).SetSelectRange(2, 2, Northwind.Categories.CategoryName).ToDataSet();

 

这里需要特别说明一下SetSelectRange(int topItemCount, int skipItemCount, QueryColumn identyColumn)方法,该方法的参数topItemCount类似,SQL中的TOP,代表返回符合条件的一定的记录数;第二个参数skipItemCount表示跳过几条符合条件的记录数,也就是说,如果skipItemCount=5,则返回从第6条符合条件的记录开始的topItemCount条记录;第三个参数是一个QueryColumn,表示该表的不可重复的主键字段。

 

如果要分页查询每页10条记录的第3页,可以指定topItemCount=10,skipItemCount=10*(PageNo - 1)=10*(3 - 1) = 20。

 

如果要返回总页数,可以先获取总的记录数,再根据每页大小计算出页数,例如:

 

int pageSize = 10;

int rowCount = (int)db.Select(Northwind.Categories, Northwind.Categories.CategoryID.Count()).ToScalar();

int pageCount = (rowCount % 10 == 0 ? rowCount / 10 : (rowCount / 10) + 1);

 

关于聚合查询返回字段的别名

 

当在Select()方法的第二个参数指定聚合函数字段如Count(),Sum()等时,注意,NBearLite在查询时会自动为聚合函数字段创建字段别名。

 

聚合函数字段别名的规则是:FUNCNAME_COLUMN_NAME。如果是Count(*),则别名为COUNT_ALL。空格会被替换为下划线(_)。

 

例如:

 

Northwind.Categories.CategoryID.Sum()别名为SUM_Categories_CategoryID

 

Inner Join查询

 

ds = db.Select(Northwind.Categories, QueryColumn.All(Northwind.Categories), QueryColumn.All(Northwind.Products))

  .Join(Northwind.Products, Northwind.Products.CategoryID == Northwind.Categories.CategoryID)

  .Where(Northwind.Categories.CategoryName.Length > 3 && Northwind.Products.UnitPrice > 100)

  .OrderBy(Northwind.Categories.CategoryID.Asc, Northwind.Products.ProductID.Desc)

  .ToDataSet();

 

自关联Inner Join查询带分页,注意自关联Join时,Join的表因为会重名,所以表明和QueryColumn都需要提供一个AliasName

 

ds = db.Select(Northwind.Categories, Northwind.Categories.__Alias("CategoriesAlias").CategoryName)

.Join(Northwind.Categories, "CategoriesAlias", Northwind.Categories.CategoryID == Northwind.Categories.__Alias("CategoriesAlias").CategoryID)

.SetSelectRange(2, 2, Northwind.Categories.CategoryID)

.Where(Northwind.Categories.CategoryName.Length > 0 && Northwind.Categories.__Alias("CategoriesAlias").Description != null)

.ToDataSet();

 

 

SubQuery查询

 

DataSet ds = db.Select(Northwind.Products)

   .Where(Northwind.Products.CategoryID.In

   (

     db.Select(Northwind.Categories, Northwind.Categories.CategoryID).SetSelectRange(10, 0, Northwind.Categories.CategoryID).ToSubQuery())

   )

   .ToDataSet();

 

ds = db.Select(Northwind.Products)

  .Where(Northwind.Products.CategoryID ==

  (

    db.Select(Northwind.Categories, Northwind.Categories.CategoryID).SetSelectRange(1, 0, Northwind.Categories.CategoryID).ToSubQuery())

  )

  .ToDataSet();