我基本上试图在条件中创建这个SQL查询。 sql查询非常简单,但要使用条件查询执行此操作,我必须从分离的条件中选择属性。任何帮助将不胜感激。
Select y0_ from
(
SELECT distinct TOP (1000) this_.Id as y0_, this_.InstrumentNumber as y1_
FROM [SearchRecorderInstrument] this_
inner join [SearchRecorderName] name1_ on this_.Id=name1_.SearchRecorderInstrument_id
inner join [NameType] nt4_ on name1_.NameType_id=nt4_.Id
inner join [SearchRecorderPropertyLegal] pl5_ on this_.Id=pl5_.SearchRecorderInstrument_id
inner join [DocumentType] dt2_ on this_.DocumentType_id=dt2_.Id
inner join [DocumentCategory] dc3_ on dt2_.DocumentCategory_id=dc3_.Id
WHERE name1_.FIPSCode = '26049' and name1_.LastName like 'smith%' and not (1=0)
ORDER BY this_.InstrumentNumber desc
) [b]
这就是我到目前为止所做的:
var criteria4 = DetachedCriteria.For(typeof(SearchRecorderInstrument), "instr");
criteria4.CreateCriteria("instr.Names", "name", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria4.CreateCriteria("instr.DocumentType", "dt", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria4.CreateCriteria("dt.DocumentCategory", "dc", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria4.CreateCriteria("name.NameType", "nt", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria4.CreateCriteria("instr.PropertyLegals", "pl", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria4.Add(Expression.Eq("name.FIPSCode", fipsCode));
criteria4.Add(Expression.Like("name.LastName", lastName, MatchMode.Start));
if (partyIndicator != null && partyIndicator > 0)
{
criteria4.Add(Expression.Eq("name.PartyIndicator", partyIndicator));
}
if (firstName != null && firstName != string.Empty)
{
criteria4.Add(Expression.Like("name.FirstName", firstName, MatchMode.Start));
}
if (beginDate != null)
{
criteria4.Add(Expression.Ge("instr.RecordedDate", beginDate.Value));
}
if (endDate != null)
{
criteria4.Add(Expression.Lt("instr.RecordedDate", endDate.Value.AddDays(1)));
}
if (consFrom != null)
{
criteria4.Add(Expression.Ge("instr.Consideration", consFrom));
}
if (consTo != null)
{
criteria4.Add(Expression.Le("instr.Consideration", consTo));
}
if (documentTypeIds != null && documentTypeIds.Any())
{
disabledDocTypes = documentTypeIds.Where(x => !disabledDocTypes.Contains(x)).ToList();
criteria4.Add(Expression.In("dt.Id", disabledDocTypes));
}
else
{
criteria4.Add(Expression.Not(Expression.In("dt.Id", disabledDocTypes)));
}
foreach (var item in legalSearchCriteria)
{
var val = item.Filter.FilterType.Value;
switch (val)
{
case "LIKE":
criteria.Add(Expression.Like("pl." + item.Filter.Field, item.Value));
break;
case ">=":
criteria.Add(Expression.Ge("pl." + item.Filter.Field, item.Value));
break;
case "<=":
criteria.Add(Expression.Le("pl." + item.Filter.Field, item.Value));
break;
case "=":
criteria.Add(Expression.Eq("pl." + item.Filter.Field, item.Value));
break;
}
}
foreach (var item in sortDescriptors)
{
if (item.SortDescriptorDirection.ToString() == "ASC")
{
criteria4.AddOrder(Order.Asc(item.ColumnName));
}
else
{
criteria4.AddOrder(Order.Desc(item.ColumnName));
}
}
criteria4.SetProjection(
Projections.ProjectionList()
.Add(Projections.Distinct(Projections.Property("instr.Id")), "InstrumentId")
.Add(Projections.Property("instr.InstrumentNumber"), "InstrumentNumber"));
criteria4.SetFirstResult(skip).SetMaxResults(rowsViewed);
我需要从此分离查询中仅选择InstrumentId。所以到目前为止我已经想出了类似的东西,但我认为这不会起作用:
var criteria5 = sess.CreateCriteria<SearchRecorderInstrument>("instr");
criteria5.Add(Subqueries.Select(criteria4));
答案 0 :(得分:0)
我假设您加入.CreateCriteria(“ForeignCollection”)
加入别名可以通过额外的重载来完成.CreateCriteria(“ForeignCollection”,“aliasname”)
在Where部分,您可以使用“aliasname”,例如“aliasname.Id”(即使这是DetachedCriteria中的名称,但是您的查询看起来不像需要分离标准)
<强>更新强>
我认为你应该考虑只选择分离标准中的仪器IDS(即标准4),而不是仪器编号。
接下来,您可以将criteria4用于子查询,例如:
Subqueries.PropertyEq("instr.InstrumentId", criteria4)
其中instr是criteria5
中的别名使用criteria5选择相应的工具数