条件查询
NHibernate.ICriteria接口表示特定持久类的一个查询。ISession是 ICriteria实例的工厂。
这里以Northwind数据库为示例数据库
示例数据表:Employees
现在只用雇员表中部分字段。
持久类如下:
public class Employees
{
public virtual int EmployeeID { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string PostalCode { get; set; }
}
映射文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain.Entities">
<class name="Employees" table="Employees">
<id name="EmployeeID" column="EmployeeID">
<generator class="identity"></generator>
</id>
<property name="LastName" column="LastName" type="String"></property>
<property name="FirstName" column="FirstName" type="String"></property>
<property name="BirthDate" column="BirthDate" type="DateTime"></property>
<property name="Address" column="Address" type="String"></property>
<property name="City" column="City" type="String"></property>
<property name="PostalCode" column="PostalCode" type="String"></property>
</class>
</hibernate-mapping>
开始
(一) 返回所有实例(返回所有雇员)
这里返回的所有实例,且是全部的属性(字段)
ICriteria crt = _session.CreateCriteria(typeof(Employees));
return crt.List<Employees>();
Isession创建条件查询实例有4个构造方法。
(二) 返回部分实例(返回2个雇员)
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.SetMaxResults(2);
return crt.List<Employees>();
(三)条件查询的约束条件
(1)Expression
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.Add(Expression.Eq("City","London"));
return crt.List<Employees>();
查询内容为:雇员的城市是在伦敦的。其中Expression的名字空间为:NHibernate.Criterion
Expression类 定义了获得某些内置ICriterion类型的工厂方法,这里用到了等于
(2)Restrictions
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.Add(Restrictions.Eq("City", "London"));
return crt.List<Employees>();
查询内容为:雇员的城市是在伦敦的。其中Restrictions的名字空间为:NHibernate.Criterion
(3)通过实例来查询
Employees ee = new Employees { City = "London", BirthDate = Convert.ToDateTime("1955-03-04 00:00:00.000") };
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.Add(Example.Create(ee));
return crt.List<Employees>();
查询伦敦的,生日在那个时间的。(为什么这里我要加个生日上去?因为我的持久类中有这个属性,如果在实例查询中不给定这个值,会有日期越界的异常。在下边的例子中,来处理这种情况)这是限制的相等的实现。下边实现一个相似的例子:
Employees ee = new Employees { FirstName = "a"};
Example exp=Example.Create(ee)
.EnableLike(MatchMode.Start)
.ExcludeProperty("BirthDate")
.IgnoreCase();
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.Add(exp);
return crt.List<Employees>();
这个例子中,指定相似的姓名等a,看实例exp:
·采用相似比较EnableLike
·相似比较匹配模式MatchMode.Start,开头匹配,这个应该类似于SQL中的a%
·排除比较属性ExcludeProperty,这个方法就是用于处理排除的。上个例子中日期没给,所以会出现异常,而这个方法则排除了这种异常发生(其实就是排除不比较的属性(字段))。
·忽略大小写
(四)排序
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.AddOrder(new NHibernate.Criterion.Order("FirstName", true));
return crt.List<Employees>();
排序字段:名字,升序(true)
(五)聚合
(1)查询人数
ICriteria crt = _session.CreateCriteria(typeof(Employees));
crt.SetProjection(Projections.RowCount());
return crt.List();
NHibernate.Expression.Projections是 IProjection 的实例工厂。通过调用 SetProjection()应用投影到一个查询。
(2)Avg
从这个开始到以下的例子又回到以Products为示例数据表
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Avg("Price"))
);
return crt.List();
通过投影列表来添加投影聚合方法。
这里取得产品的平均价格,这里没有条件约束,下边这个例子取得产品类别为2的产品的平均价格:
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Avg("Price")))
.Add(Expression.Eq("CategoryID",2));
return crt.List();
(3)Max(最大价格)
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Max("Price")));
return crt.List();
(4)Min(最低价格)
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Min ("Price")));
return crt.List();
(5)Sum(和)
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.Sum ("Price")));
return crt.List();
(6)分组
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("CategoryID")));
return crt.List<int>();
这个分组只是返回一个属性,所以用int泛型可以了。下边的例子返回分组,并返回各组的数目
ICriteria crt = _session.CreateCriteria(typeof(Products));
crt.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("CategoryID"))
.Add(Projections.RowCount()));
return crt.List();
·这里的List为System.Collections.Ilist,且是object[]类型的
分享到:
相关推荐
1 Introduction – a Tour of Multiple View Geometry 1 1.1 Introduction – the ubiquitous projective geometry 1 1.2 Camera projections 6 1.3 Reconstruction from more than one view 10 1.4 Three-view ...
- **摄像机投影(Camera Projections)**:摄像机模型和投影理论是构建三维重建的基础。通过理解如何将三维空间中的物体投影到二维平面上,可以推导出各种有用的关系式,如基础矩阵(fundamental matrix)和本质矩阵...
Multiple Random Projections (MRP) is our formerly presented two-factor cancellable formulation. In that method, the biometric data is changed in a revocable but non-invertible manner by projecting ...
Abstract—In this paper, an active contour model is proposed for image segmentation based on multiple distribution on each region with adaptive weight between them. The distribution are drawn...
### 五、组合多个投影(Combining Multiple Projections) 可以使用`ProjectionList`来组合多个投影。 **示例代码**: ```java ProjectionList projectionList = Projections.projectionList(); projectionList....
You'll master core practices like handling multiple vector file formats, editing and manipulating geometries, applying spatial and attribute filters, working with projections, and performing basic ...
We cast the recognition problem as one of classifying among multiple linear regression models and argue that new theory from sparse signal representation offers the key to addressing this problem. ...
多面支持向量机(multiple surface support vector machine,MSSVM)分类方法作为传统支持向量机(support vector machine,SVM)的拓展在模式识别领域成为新的研究热点之一,然而已有的MSSVM方法并没有充分考虑到训练样本...
9. **Projection Pursuit**: A method for finding low-dimensional projections of high-dimensional data that maximize certain measures of interest, such as non-Gaussianity. 10. **Neural Networks**: A ...
展览特色在于通过高科技手段,如高清投影(High-definition projections)和增强现实(Augmented reality, AR)技术,创造了一种沉浸式的艺术体验。参观者可以环绕在巨大屏幕上展示的梵高画作之中,比如《向日葵》和...
Dynamic stream switching is an efficient scheme which stores multiple asymmetric projections of panoramic video in server side, and transmits only one asymmetric projection (with much lower ...
- **Type Projections**: Describes how to transform collections into new types using LINQ. - **Extension Methods**: Enables developers to add methods to existing types without creating a new derived ...
研究结果表明,通过全透射高光谱成像技术,结合不同类型的模型——偏最小二乘法(Partial Least Squares,简称PLS)、多元线性回归(Multiple Linear Regression,简称MLR)和支持向量机(Support Vector Machine,...
- **彭博分红预测 (Bloomberg Dividend Projections, BDVD*)** - 对公司分红情况的预测。 - **分红/拆股信息 (Dividend/Split Information, DVD*)** - 记录公司的分红和拆股历史。 - **比较回报 (Comparative ...
13.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 13.8. 离线(detached)查询和子查询 14. 原生SQL查询 14.1. 使用ISQLQuery 14.1.1. 标量查询(Scalar queries) 14.1.2. 实体查询(Entity queries) ...
13.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 13.8. 离线(detached)查询和子查询 14. 原生SQL查询 14.1. 使用ISQLQuery 14.1.1. 标量查询(Scalar queries) 14.1.2. 实体查询(Entity queries) ...
15.7.1 Single and Multiple Fracture 795 15.7.2 Failure Modes in Composites 796 15.8 Some Fundamental Characteristics of Composites 799 15.8.1 Heterogeneity 799 15.8.2 Anisotropy 799 15.8.3 Shear ...