0
我基本上試圖在條件中創建這個SQL查詢。 SQL查詢很容易,但要做到這一點與標準查詢我必須從分離標準中選擇一個屬性。任何幫助將不勝感激。從NHibernate的分離條件選擇別名屬性
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));
我已經編輯我的問題有點,即時通訊不知道如何使用我的標準5查詢中分離標準的別名來選擇我的標準4分離標準之外的屬性。如果你可以把它放在一個很好的例子中。謝謝 – Noah 2013-02-19 17:00:10
我希望我能做到這一點,但爲了做一個獨特的instrumentId和訂單的儀器數量,我必須在選擇包括兩個屬性。 – Noah 2013-02-19 19:18:18
好吧,回到開始那麼:什麼是查詢的最終目的?你想從數據庫中取出什麼? – 2013-02-19 20:14:26