基本查询

 

复杂查询示例

/// <summary>
/// 获取自定义表单数据中属于部门的部分
/// </summary>
/// <param name="month"></param>
/// <param name="departmentId"></param>
/// <param name="positionId"></param>
/// <param name="fillUserName"></param>
/// <param name="departmentName"></param>
/// <param name="formName"></param>
/// <param name="fieldName"></param>
/// <param name="assesserName"></param>
/// <returns></returns>
public IList<CustomFormResultItem> GetDepartmentCustomFormItems(DateTime month, Guid? customFormId, Guid? departmentId, string fillUserName = "", string departmentName = "", string formName = "", string fieldName = "", string assesserName = "")
{
return GetCustomFormItemInternal(2, month, customFormId, departmentId, null, fillUserName, departmentName, formName, fieldName, assesserName);
} public IList<CustomFormResultItem> GetCustomFormItemInternal(int? userorDeptControlType, DateTime month, Guid? customFormId, Guid? departmentId, Guid? positionId,
string fillUserName = "", string belongToUserOrDepartmentName = "", string formName = "", string fieldName = "", string assesserName = "")
{
var query = _customFormResultItemRep.CreateCriteriaQuery()
.CreateAlias("t.Result", "result") .CreateAlias("result.CreateUser", "createUser")
.CreateAlias("t.FormItem", "formItem")
.CreateAlias("result.CustomForm", "customForm"); if (month == DateTime.MinValue)
month = DateTime.Now; //Filter by month
query.Add(Restrictions.Eq(Projections.SqlFunction(new SQLFunctionTemplate(NHibernateUtil.Int16, "datediff(month,?1,?2)"),
NHibernateUtil.Int16, Projections.Property("CreateDate"), Projections.Constant(month)), 0)); if (userorDeptControlType.HasValue)
{
if (userorDeptControlType == (int)FormControl.姓名)
{
query.CreateAlias("result.BelongUser", "belongUser");
if (departmentId.HasValue && departmentId != Guid.Empty)
{
query.Add(Restrictions.Eq("belongUser.Department.Id", departmentId));
}
if (positionId.HasValue && positionId != Guid.Empty)
{
query.Add(Restrictions.Eq("belongUser.Position.Id", positionId));
}
if (!string.IsNullOrWhiteSpace(belongToUserOrDepartmentName))
{
query.Add(Restrictions.Like("belongUser.Name", belongToUserOrDepartmentName, MatchMode.Anywhere));
}
}
else if (userorDeptControlType == (int)FormControl.部门)
{
query.CreateAlias("result.BelongDepartment", "belongDepartment"); if (departmentId.HasValue && departmentId != Guid.Empty)
{
query.Add(Restrictions.Eq("belongDepartment.Id", departmentId));
}
//if (positionId.HasValue && positionId != Guid.Empty)
//{
// query.Add(Restrictions.Eq("belongUser.Position.Id", positionId));
//}
if (!string.IsNullOrWhiteSpace(belongToUserOrDepartmentName))
{
query.Add(Restrictions.Like("belongDepartment.Name", belongToUserOrDepartmentName, MatchMode.Anywhere));
}
} var customFormSubQuery = DetachedCriteria.For<CustomFormItem>("cfi")
.CreateAlias("CustomForm", "cf")
.Add(Restrictions.Eq("cfi.FieldType", (FormControl)userorDeptControlType))
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetProjection(Projections.Property("cf.Id")); query.Add(Subqueries.PropertyIn("customForm.Id", customFormSubQuery)); if (userorDeptControlType == (int)FormControl.姓名)
{
query.AddOrder(new Order("belongUser.Name", true));
}
else
{
query.AddOrder(new Order("belongDepartment.Name", true));
}
} if (!string.IsNullOrWhiteSpace(fillUserName))
{
query.Add(Restrictions.Like("createUser.Name", fillUserName, MatchMode.Anywhere));
}
if (!string.IsNullOrWhiteSpace(formName))
{
query.Add(Restrictions.Like("customForm.Name", formName, MatchMode.Anywhere));
}
if (customFormId != null && customFormId != Guid.Empty)
{
query.Add(Restrictions.Eq("customForm.Id", customFormId));
} if (!string.IsNullOrWhiteSpace(fieldName))
{
query.Add(Restrictions.Like("t.FieldName", fieldName, MatchMode.Anywhere));
}
if (!string.IsNullOrEmpty(assesserName))
{
query.Add(Restrictions.Like("createUser.Name", assesserName, MatchMode.Anywhere));
} var types = new List<FormControl>() { FormControl.单选, FormControl.多选, FormControl.引用部门, FormControl.指标标准, FormControl.数字框, FormControl.文本框, FormControl.日期, FormControl.段落 }.ToArray();
if (types.Length > 0)
query.Add(Restrictions.In("formItem.FieldType", types)); query.AddOrder(new Order("result.Id", true));
query.AddOrder(new Order("formItem.OrderByIndex", true)); query.SetMaxResults(100); var resultList = query.List<CustomFormResultItem>();
return resultList;
}

 

数据库函数查询示例

public IList<DepartmentIndicatorResult> SearchDepartmentIndicatorResult(DateTime evaluateMonth, Guid? departmentId, string indicatorName, string departmentName)
{
var query = this._departmentIndicatorResultRep.CreateCriteriaQuery().CreateAlias("t.Department", "dept").CreateAlias("t.Indicator", "indicator"); if (evaluateMonth == DateTime.MinValue)
evaluateMonth = DateTime.Now;
//Filter by month
query.Add(Restrictions.Eq(Projections.SqlFunction(new SQLFunctionTemplate(NHibernateUtil.Int16, "datediff(month,?1,?2)"), NHibernateUtil.Int16, Projections.Property("PointDate"), Projections.Constant(evaluateMonth)), 0));
//filter by day
//query.Add(Restrictions.Eq(Projections.SqlFunction(new SQLFunctionTemplate(NHibernateUtil.Int16, "datediff(day,?1,?2)"), NHibernateUtil.Int16, Projections.Property("PointDate"), Projections.Constant(evaluateMonth)), 0)); if (departmentId.HasValue && departmentId != Guid.Empty)
{
query.Add(Restrictions.Eq("user.Department.Id", departmentId));
} if (!string.IsNullOrWhiteSpace(departmentName))
{
query.Add(Restrictions.Like("dept.Name", departmentName, MatchMode.Anywhere));
}
if (!string.IsNullOrWhiteSpace(indicatorName))
{
query.Add(Restrictions.Like("indicator.Name", indicatorName, MatchMode.Anywhere));
}
//query.Add(Restrictions.Eq("indicator.DataCollectorType", DataCollectorType.月底收集)); query.SetMaxResults(100);
//query.AddOrder(new Order("pos.Name", true)); var resultList = query.List<DepartmentIndicatorResult>();
return resultList;
}

查询示例展示

/// <summary>
/// 查询员工指标考核结果(日评,月底指标考核成绩)
/// </summary>
/// <param name="dateRangeType"></param>
/// <param name="date"></param>
/// <param name="departmentId"></param>
/// <param name="positionId"></param>
/// <param name="indicatorName"></param>
/// <param name="userName"></param>
/// <param name="dataCollectorTypes"></param>
/// <returns></returns>
private IList<UserIndicatorResult> SearchUserIndicatorResult(DateRangeType dateRangeType, DateTime date, Guid? departmentId = null, Guid? positionId = null, string indicatorName = null,
string userName = null, IList<DataCollectorType> dataCollectorTypes = null, IList<DataInputType> dataInputTypes = null, Guid? userId = null, Guid? indicatorId = null, int limit = 200, bool isAssess = true)
{
var query = this._userIndicatorResultRep.CreateCriteriaQuery().CreateAlias("t.User", "user").CreateAlias("t.Indicator", "indicator")
.Add(Restrictions.Eq("t.DateRangeType", dateRangeType)); if (date == DateTime.MinValue)
date = DateTime.Now;
if (dateRangeType == DateRangeType.Monthly || (dataCollectorTypes != null && dataCollectorTypes.Contains(DataCollectorType.月底收集)))
{
//Filter by month
query.Add(Restrictions.Eq(Projections.SqlFunction(new SQLFunctionTemplate(NHibernateUtil.Int16, "datediff(month,?1,?2)"), NHibernateUtil.Int16, Projections.Property("PointDate"), Projections.Constant(date)), 0));
}
else if (dateRangeType == DateRangeType.Daily)
{
//filter by day
query.Add(Restrictions.Eq(Projections.SqlFunction(new SQLFunctionTemplate(NHibernateUtil.Int16, "datediff(day,?1,?2)"), NHibernateUtil.Int16, Projections.Property("PointDate"), Projections.Constant(date)), 0));
} if (departmentId.HasValue && departmentId != Guid.Empty)
{
query.Add(Restrictions.Eq("user.Department.Id", departmentId));
}
if (positionId.HasValue && positionId != Guid.Empty)
{
query.Add(Restrictions.Eq("user.Position.Id", positionId));
}
if (isAssess)
{
//todo
query.Add(Restrictions.IsNotNull("TextResult"));
}
else
{
query.Add(Restrictions.IsNull("TextResult"));
}
if (!string.IsNullOrWhiteSpace(userName))
{
query.Add(Restrictions.Like("user.Name", userName, MatchMode.Anywhere));
}
if (userId.HasValue)
{
query.Add(Restrictions.Eq("user.Id", userId.Value));
}
if (indicatorId.HasValue)
{
query.Add(Restrictions.Eq("indicator.Id", indicatorId.Value));
}
if (!string.IsNullOrWhiteSpace(indicatorName))
{
query.Add(Restrictions.Like("indicator.Name", indicatorName, MatchMode.Anywhere));
} if (dataCollectorTypes != null)
query.Add(Restrictions.In("indicator.DataCollectorType", dataCollectorTypes.ToArray()));
if (dataInputTypes != null)
query.Add(Restrictions.In("indicator.DataInputType", dataInputTypes.ToArray())); //query.Add(Restrictions.Eq("indicator.DataCollectorType", DataCollectorType.月底收集)); //为了性能的考虑,每次值允许拉200条记录
query.SetMaxResults(limit);
query.AddOrder(new Order("user.Name", true)); var resultList = query.List<UserIndicatorResult>();
return resultList;
}

 

public IList<PositionIndicator> SearchPositionIndicatorList(
List<Guid> departmentIds = null, Guid? departmentId = null, string departmentName = null,
List<Guid> positionIds = null, Guid? positionId = null, string positionName = null,
List<Guid> linkedFormIds = null, Guid? linkedFormId = null,
string indicatorName = null,
EvaluateType? evaluateType = null,
string dataSource = null,
string discriminant = null,
Guid? crossAssesserId = null,
string crossAssesserName = null,
List<DataCollectorType> dataCollectorTypes = null, DataCollectorType? dataCollectorType = null,
int limit = 200)
{
var query = CreateCriteriaQuery<PositionIndicator>()
.CreateAlias("t.Position", "pos").CreateAlias("t.CrossAssesser", "ca", JoinType.LeftOuterJoin); #region Special Query
if (departmentIds != null || departmentId.HasValue)
{
if (departmentIds == null)
departmentIds = new List<Guid>();
if (departmentId.HasValue)
departmentIds.Add(departmentId.Value); var positionSubquery = DetachedCriteria.For<Department>()
.CreateAlias("Positions", "pos", NHibernate.SqlCommand.JoinType.InnerJoin)
.Add(Restrictions.In("Id", departmentIds.ToArray()))
.SetProjection(Projections.Property("pos.Id")); query.Add(Subqueries.PropertyIn("pos.Id", positionSubquery));
} if (!string.IsNullOrWhiteSpace(departmentName))
{
var positionSubquery = DetachedCriteria.For<Department>()
.CreateAlias("Positions", "pos", NHibernate.SqlCommand.JoinType.InnerJoin)
.Add(Restrictions.Like("Name", departmentName, MatchMode.Anywhere))
.SetProjection(Projections.Property("pos.Id")); query.Add(Subqueries.PropertyIn("pos.Id", positionSubquery));
} if (positionIds != null || positionId.HasValue)
{
if (positionIds == null)
positionIds = new List<Guid>();
if (positionId.HasValue)
positionIds.Add(positionId.Value);
if (positionIds.Count > 0)
query.Add(Restrictions.In("t.Position.Id", positionIds.ToArray()));
} if (!string.IsNullOrWhiteSpace(positionName))
{
query.Add(Restrictions.Like("t.Position.Name", positionName, MatchMode.Anywhere));
}
#endregion BindIndicatorQuery(query, linkedFormIds, linkedFormId, indicatorName, evaluateType, dataSource, discriminant, crossAssesserId, crossAssesserName, dataCollectorTypes, dataCollectorType, limit); return query.List<PositionIndicator>();
} public IList<DeptIndicator> SearchDepartmentIndicatorList(
List<Guid> departmentIds = null, Guid? departmentId = null, string departmentName = null,
List<Guid> linkedFormIds = null, Guid? linkedFormId = null,
string indicatorName = null,
EvaluateType? evaluateType = null,
string dataSource = null,
string discriminant = null,
Guid? crossAssesserId = null,
string crossAssesserName = null,
List<DataCollectorType> dataCollectorTypes = null, DataCollectorType? dataCollectorType = null,
int limit = 200)
{
var query = CreateCriteriaQuery<DeptIndicator>().CreateAlias("t.CrossAssesser", "ca");
if (departmentIds != null || departmentId.HasValue)
{
if (departmentIds == null)
departmentIds = new List<Guid>();
if (departmentId.HasValue)
departmentIds.Add(departmentId.Value);
if (departmentIds.Count > 0)
query.Add(Restrictions.In("t.Department.Id", departmentIds.ToArray()));
} BindIndicatorQuery(query, linkedFormIds, linkedFormId, indicatorName, evaluateType, dataSource, discriminant, crossAssesserId, crossAssesserName, dataCollectorTypes, dataCollectorType, limit); return query.List<DeptIndicator>();
} private static void BindIndicatorQuery(ICriteria query,
List<Guid> linkedFormIds = null, Guid? linkedFormId = null,
string indicatorName = null,
EvaluateType? evaluateType = null,
string dataSource = null,
string discriminant = null,
Guid? crossAssesserId = null,
string crossAssesserName = null,
List<DataCollectorType> dataCollectorTypes = null, DataCollectorType? dataCollectorType = null,
int limit = 200)
{
if (linkedFormIds != null || linkedFormId.HasValue)
{
if (linkedFormIds == null)
linkedFormIds = new List<Guid>();
if (linkedFormId.HasValue)
linkedFormIds.Add(linkedFormId.Value);
if (linkedFormIds.Count > 0)
query.Add(Restrictions.In("t.Form.Id", linkedFormIds.ToArray()));
}
if (!string.IsNullOrWhiteSpace(indicatorName))
{
query.Add(Restrictions.Like("t.Name", indicatorName, MatchMode.Anywhere));
}
if (evaluateType.HasValue)
{
query.Add(Restrictions.Eq("t.EvaluateType", evaluateType));
}
if (!string.IsNullOrWhiteSpace(dataSource))
{
query.Add(Restrictions.Like("t.DataSource", dataSource, MatchMode.Anywhere));
}
if (!string.IsNullOrWhiteSpace(discriminant))
{
query.Add(Restrictions.Like("t.Discriminant", discriminant, MatchMode.Anywhere));
}
if (!string.IsNullOrWhiteSpace(crossAssesserName))
{
query.Add(Restrictions.Like("ca.Name", crossAssesserName, MatchMode.Anywhere));
}
if (crossAssesserId.HasValue)
{
query.Add(Restrictions.Eq("t.CrossAssesser.Id", crossAssesserId));
}
if (dataCollectorTypes != null || dataCollectorType.HasValue)
{
if (dataCollectorTypes == null)
dataCollectorTypes = new List<DataCollectorType>();
if (dataCollectorType.HasValue)
dataCollectorTypes.Add(dataCollectorType.Value); if (dataCollectorTypes.Count > 0)
query.Add(Restrictions.In("t.DataCollectorType", dataCollectorTypes.ToArray()));
}
query.SetMaxResults(limit);
}

NHibernate查询示例合集的更多相关文章

  1. 天气类API调用的代码示例合集:全国天气预报、实时空气质量数据查询、PM2.5空气质量指数等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 全国天气预报:数据来自国家气象局,可根据地名.经纬度GPS.IP查 ...

  2. 位置信息类API调用的代码示例合集:中国省市区查询、经纬度地址转换、POI检索等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 中国省市区查询:2017最新中国省市区地址 经纬度地址转换:经纬度 ...

  3. 通讯服务类API调用的代码示例合集:短信服务、手机号归属地查询、电信基站查询等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 短信服务:通知类和验证码短信,全国三网合一通道,5秒内到达,费用低 ...

  4. 生活常用类API调用的代码示例合集:邮编查询、今日热门新闻查询、区号查询等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 邮编查询:通过邮编查询地名:通过地名查询邮编 今日热门新闻查询:提 ...

  5. 出行服务类API调用的代码示例合集:长途汽车查询、车型大全、火车票查询等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 长途汽车查询:全国主要城市的长途汽车时刻查询,汽车站查询 车型大全 ...

  6. 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...

  7. vue指令示例合集

    vue所有指令练习合集.这是个html文件,用chrome打开可查看结果. <!DOCTYPE html> <html lang="en" xmlns:v-on= ...

  8. FFmpeg示例程序合集-批量编译脚本

    此前做了一系列有关FFmpeg的示例程序,组成了<最简单的FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player:                   ...

  9. FFmpeg示例程序合集-Git批量获取脚本

    此前做了一系列有关FFmpeg的示例程序,组成了<FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player:                  最简单的 ...

随机推荐

  1. 实战系列之 Node.js 玩转 Java

    这些年以来,Node.js的兴起,JavaScript已经从当年的“世界最被误解的语言”变成了“世界最流行的语言”.且其发展之势,从语言本身的进化,库和包的增长,工具支持的完善,star项目和领域解决 ...

  2. An Introduction to Variational Methods (5.3)

    从之前的文章中,我们已经得到了所有需要求解的参数的优化分布的形式,分别为: ‍ 但是,我们从这些分布的表达式中(参见之前的文章),可以发现这些式子并不能够直接求解.这是因为各个参数之间相互耦合,从而导 ...

  3. 机器学习实战K-近邻算法

    今天开始学习机器学习,第一章是K-近邻算法,有不对的地方请指正 大概总结一下近邻算法写分类器步骤: 1. 计算测试数据与已知数据的特征值的距离,离得越近越相似 2. 取距离最近的K个已知数据的所属分类 ...

  4. TargetType Mismatch

    TargetType Mismatch 环境:windowsphone 8,silerlight toolkit, 页面报TargeType Mismatch错误或者 length 0,是因为Syst ...

  5. Ubuntu16.04 install eclipse-jee-oxygen-R-linux-gtk-x86_64

    下面如何在Ubuntu16.04 下面怎么下载Java EE并创建在桌面快捷上下载Java EE:eclipse下载Java EE官网:http://www.eclipse.org/downloads ...

  6. 自动化selenium开发

    一.开发环境搭建 1.Firefox浏览器 1.1 下载firefix并安装. 1.2 Firefox中打开"开始菜单“ -> ”开发者“ -> ”获取更多工具“ -> 搜 ...

  7. 用FastDFS一步步搭建文件管理系统

    一.FastDFS介绍 FastDFS开源地址:https://github.com/happyfish100 参考:分布式文件系统FastDFS设计原理 参考:FastDFS分布式文件系统 个人封装 ...

  8. PE格式第九讲,资源表解析

    PE格式第九讲,资源表解析 一丶熟悉Windows管理文件的方法 首先,为什么标题是这个,主要是为了下边讲解资源方便,因为资源结构体很乱.如果直接拿出来讲解,那么就会很晕. 1.windows管理文件 ...

  9. 正六边形网格化(Hexagonal Grids)原理与实现

    在路径规划.游戏设计栅格法应用中,正六边形网格不如矩形网格直接和常见,但是正六边形具有自身的应用特点,更适用于一些特殊场景中,比如旷阔的海洋.区域或者太空.本文主要讲述如何对正六边形进行几何学分析.网 ...

  10. eclipse创建一个文件夹

    如何给eclipse创建一个文件夹,便于项目的管理:有时我们的eclipse中会有很多项目的,有的是公司的如Project1,Project2,Project3....还有的呢, 也可能是自己平时做的 ...