私は基本的に、基準でこの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);
この Detached クエリから InstrumentId だけを選択する必要があります。そして、これまでのところこのようなものを思いつきましたが、これがうまくいくとは思いません:
var criteria5 = sess.CreateCriteria<SearchRecorderInstrument>("instr");
criteria5.Add(Subqueries.Select(criteria4));