NHibernate:LINQ to NHibernate 自连接
NHibernate: LINQ to NHibernate self join
我正在尝试使用 LINQ to NHibernate 实现以下 SQL 语句:
SELECT a.JOB_ID ID FROM SERVICE_DATA a WHERE STEP_ID = x and (STATUS = 'Success' or STATUS = 'Skipped')
AND a.JOB_ID not in (SELECT JOB_ID FROM SERVICE_DATA WHERE JOB_ID = a.JOB_ID AND STATUS = 'Error')
AND a.JOB_ID not in (SELECT JOB_ID FROM SERVICE_DATA WHERE STEP_ID = y and (STATUS = 'Success' or STATUS = 'Skipped' or STATUS = 'Error'));
目前我能够 select 一切都期待 1st NOT IN self join 条件使用:
DetachedCriteria secondSubCriteria = DetachedCriteria.For<ServiceData>()
.SetProjection(Projections.Property("JobId"))
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped"))
.Add(Restrictions.Eq("Status", "Error")));
var data = session.CreateCriteria<ServiceData>()
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped")))
//.Add(Subqueries.PropertyNotIn("JobId", ???))
.Add(Subqueries.PropertyNotIn("JobId", secondSubCriteria))
.List<ServiceData>()
.Take(1)
.FirstOrDefault();
如何让基于列 JOB_ID 的自连接在示例中的第一个子标准上工作?
在尝试之后我自己找到了答案,对于任何遇到同样问题的人,请在 Criteria() 构造函数中指定一个 table 别名,并在其他查询中引用这个。
DetachedCriteria firstSubCriteria = DetachedCriteria.For<ServiceData>("x")
.SetProjection(Projections.Property("x.JobId"))
.Add(Restrictions.EqProperty("x.JobId", "s.JobId"))
.Add(Restrictions.Not(Restrictions.Eq("x.Status", "Error")));
DetachedCriteria secondSubCriteria = DetachedCriteria.For<ServiceData>()
.SetProjection(Projections.Property("JobId"))
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped"))
.Add(Restrictions.Eq("Status", "Error")));
var data = session.CreateCriteria<ServiceData>("s")
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("s.Status", "Success"))
.Add(Restrictions.Eq("s.Status", "Skipped")))
.Add(Subqueries.PropertyNotIn("s.JobId", firstSubCriteria))
.Add(Subqueries.PropertyNotIn("s.JobId", secondSubCriteria))
.List<ServiceData>()
.Take(1)
.FirstOrDefault();
我正在尝试使用 LINQ to NHibernate 实现以下 SQL 语句:
SELECT a.JOB_ID ID FROM SERVICE_DATA a WHERE STEP_ID = x and (STATUS = 'Success' or STATUS = 'Skipped')
AND a.JOB_ID not in (SELECT JOB_ID FROM SERVICE_DATA WHERE JOB_ID = a.JOB_ID AND STATUS = 'Error')
AND a.JOB_ID not in (SELECT JOB_ID FROM SERVICE_DATA WHERE STEP_ID = y and (STATUS = 'Success' or STATUS = 'Skipped' or STATUS = 'Error'));
目前我能够 select 一切都期待 1st NOT IN self join 条件使用:
DetachedCriteria secondSubCriteria = DetachedCriteria.For<ServiceData>()
.SetProjection(Projections.Property("JobId"))
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped"))
.Add(Restrictions.Eq("Status", "Error")));
var data = session.CreateCriteria<ServiceData>()
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped")))
//.Add(Subqueries.PropertyNotIn("JobId", ???))
.Add(Subqueries.PropertyNotIn("JobId", secondSubCriteria))
.List<ServiceData>()
.Take(1)
.FirstOrDefault();
如何让基于列 JOB_ID 的自连接在示例中的第一个子标准上工作?
在尝试之后我自己找到了答案,对于任何遇到同样问题的人,请在 Criteria() 构造函数中指定一个 table 别名,并在其他查询中引用这个。
DetachedCriteria firstSubCriteria = DetachedCriteria.For<ServiceData>("x")
.SetProjection(Projections.Property("x.JobId"))
.Add(Restrictions.EqProperty("x.JobId", "s.JobId"))
.Add(Restrictions.Not(Restrictions.Eq("x.Status", "Error")));
DetachedCriteria secondSubCriteria = DetachedCriteria.For<ServiceData>()
.SetProjection(Projections.Property("JobId"))
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Status", "Success"))
.Add(Restrictions.Eq("Status", "Skipped"))
.Add(Restrictions.Eq("Status", "Error")));
var data = session.CreateCriteria<ServiceData>("s")
.Add(Restrictions.Eq("StepId", stepId))
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("s.Status", "Success"))
.Add(Restrictions.Eq("s.Status", "Skipped")))
.Add(Subqueries.PropertyNotIn("s.JobId", firstSubCriteria))
.Add(Subqueries.PropertyNotIn("s.JobId", secondSubCriteria))
.List<ServiceData>()
.Take(1)
.FirstOrDefault();