带子选择的复杂查询

时间:2012-04-02 18:35:14

标签: nhibernate linq-to-nhibernate

我已经开始在Linq中编写一个如下所示的查询:

var t = (from employee in _session.Query<Employee>()
                 join logon in _session.Query<WinLogon>() on employee.logon equals logon
                 join states in _session.Query<AppState>() on logon equals states.logon
                 join leader in _session.Query<Employee>() on employee.leader equals leader
                 group new 
                 { 
                     employee, states, logon, leader 
                 } 
                 by new 
                 { 
                     employee_id=employee.id, 
                     employee.first_name, employee.last_name, 
                     leader_1 = leader.first_name,
                     leader_2 = leader.last_name,
                 } 
                 into grouped
                 select new WorkerSummary
                 {
                     FullName = grouped.Key.first_name+" "+grouped.Key.last_name,
                     LeaderFullName = grouped.Key.leader_1+" "+grouped.Key.leader_2,
                     LogonTime = grouped.Min(a=>a.states.probe_time).ToString(),

                     WorkstationName = (from events in _session.Query<Event>()
                             where events.event_type.id == 1
                             select new { events.event_stamp }).FirstOrDefault().event_stamp.ToString(),

                     WorkDuration = ""+(grouped.Max(a => a.states.probe_time) - grouped.Min(a=>a.states.probe_time)),
                     ManualStatus = "" + grouped.Count()
                 }


                 ).ToList<WorkerSummary>();

但我注意到left join和像WorkstationName = (from events...这样的子选择不适用于NH Linq。因为在这个查询中会有更多的子选择,所以我决定切换Criteria或HQL。

我做错了什么,因为我写了子选择和如此大的查询? 是否有任何众所周知的做法在Linq中编写查询?

0 个答案:

没有答案