2011-07-03 85 views
1

如何使用Castle ActiveRecords和LINQ或HQL執行以下查詢?如何使用LINQ或HQL執行以下SQL查詢

SELECT a.id,s.classes,COUNT(p.id),MAX(p.date)截至去年,MIN(p.date)爲第一
FROM帳戶
LEFT JOIN校補ON s.account_id = a.id
LEFT JOIN用戶U ON u.account_id = a.id
LEFT JOIN點p的p.user_id = u.id
WHERE支付= 「S」
GROUP BY一個。編號

這些表是相關的我n以下面的方式: ER-Diagramm

我也有ActiveRecord類的所有表定義正確的關係(如果我做查詢步驟它的工作,但它很慢,因爲有很多行),我試過了這在投擲以下The method or operation is not implemented -Exception

var result = from account in AccountRecord.Queryable 
      join s in SchoolRecord.Queryable on account equals s.Account into schools 
      from school in schools.DefaultIfEmpty(null) 
      join user in UserRecord.Queryable on account equals user.Account 
      join p in PointsRecord.Queryable on user equals p.User into points 
      where account.PaymentType == "S" 
      select new { Account = account, School = school, Count = points.Count() }; 

:這不工作的以下

NHibernate.Linq.Visitors.QueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause,QueryModel queryModel,的Int32指數)

+0

你不必按單曲做。類'要麼得到一個有效的SQL查詢? –

+0

不,'s.classes'只是學校表中的一個整列,表示學校的班級數量。我實際上需要帳戶和學校表中的更多列,我在這裏將其留給了主要問題,但SQL查詢在發佈時直接對de DB執行。 – ChrFin

回答

1

發現使用HQL解決方案 - 我還是開了LINQ的解決方案:

HqlBasedQuery query = new HqlBasedQuery(typeof(AccountRecord), 
    "SELECT a, s, COUNT(p), MIN(p.DateUTC), MAX(p.DateUTC) " + 
    "FROM AccountRecord a " + 
    "LEFT JOIN a.Schools s " + 
    "LEFT JOIN a.Users u " + 
    "LEFT JOIN u.Points p " + 
    "WHERE a.PaymentType=:payment GROUP BY a.Id"); 
query.SetParameter("payment", "S"); 
var result = from object[] row in (ArrayList)ActiveRecordMediator.ExecuteQuery(query) 
       select new 
       { 
        Account = row[0] as AccountRecord, 
        School = row[1] as SchoolRecord, 
        Count = row[2], 
        First = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[3])), 
        Last = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[4])) 
       }; 
+0

我現在要接受這個/我的解決方案,因爲它的onyl工作解決方案我有自動取款機,但我仍然喜歡使用LINQ ... – ChrFin

0

我覺得你的Linq查詢會是這樣的:

var result = from a in AccountRecord.Queryable 
      join s in SchoolRecord.Queryable on a.id equals s.account_id 
      join u in UserRecord.Queryable on a.id equals u.account_id 
      join p in PointsRecord.Queryable on u.id equals p.user_id 
      where a.payment == "S" 
      group by a.id 
      select new 
      { 
       Account = a, 
       School = s, 
       Count = p.Count() 
      }; 

雖然我我不確定NHibernate會如何處理group byCount()的組合。如果不產生錯誤,您可能想要查看生成的SQL最終的結果。

如果不工作,你可能想選擇的記錄後面,和組/應用程序盡數相反,更像是:

var data = from a in AccountRecord.Queryable 
      join s in SchoolRecord.Queryable on a.id equals s.account_id 
      join u in UserRecord.Queryable on a.id equals u.account_id 
      join p in PointsRecord.Queryable on u.id equals p.user_id 
      where a.payment == "S" 
      select new 
      { 
      Account = a, 
      School = s, 
      Count = c 
      }; 
var grouped = data.ToList.GroupBy(x => x.Account.Id); 
+0

對不起,對於遲到的答案,但是這會引發我一個'無法投射類型爲'Remotion.Data.Linq.Clauses.JoinClause'的對象來輸入'Remotion.Data.Linq.Clauses.FromClauseBase'.' Exception ... – ChrFin