如何通过 linq 查询 select 流利的 nHibernate 中最少的项目数?
How to select the least count of items in fluent nHibernate via a linq query?
所以我得到了一个流畅的 nHibernate table 像这样:
public class AnsweredQuestionDb
{
public virtual long Id { get; set; }
public virtual QuestionDb Question { get; set; }
public virtual AnswerDb Answer { get; set; }
}
QuestionDb 和 AnswerDb 是其他流利的 nHibernate tables.
所以我想 select 回答最少的问题。因此,如果问题 A 被回答了 3 次,问题 B、C 和 D 回答了 4 次,结果应该是问题 A。
这就是我实际工作的重点:
var leastAnswerdQuestion = (from t in m_dbSession.Query<AnsweredQuestionDb>()
group t.Id by t.Question into groups
select groups.ToList().Sum()).ToList()[0];
但是这个抛出异常,里面没有or order by。这甚至可以在一次查询中完成吗?
我希望清楚我想做什么。
我假设在 sql 中看起来像这样:
SELECT Question_id, count(Question_id)
FROM AnsweredQuestionDb
GROUP BY Question_id
ORDER By count(Question_id)
所以将 LINQ 查询写成 SQL:
var grouped =
from t in m_dbSession.Query<AnsweredQuestionDb>()
group t by t.Question.Id into g
select new
{
QuestionId = g.Key,
Count = g.Count()
};
var leastAnsweredId = grouped
.OrderBy(x => x.Count)
.Select(x => x.QuestionId)
.FirstOrDefault();
因此,在 Svyatoslav Danyliv 的帮助下,我创建了这个:
当然这并不完美,但它是有目的的:
QuestionDb nextQuestion = null;
var grouped = (from t in m_dbSession.Query<AnsweredQuestionDb>()
group t by t.Question.Id into g
orderby g.Count()
select new
{
QuestionId = g.Key,
Count = g.Count()
}).ToList();
var keys = grouped.Select(o => o.QuestionId).ToList();
var notAnswered = (from t in m_dbSession.Query<QuestionDb>()
where
!keys.Contains(t.Id)
select t).ToList().First();
if (notAnswered != null)
{
nextQuestion = notAnswered;
}
else
{
if (grouped.Count() == 0)
{
nextQuestion = (from t in m_dbSession.Query<QuestionDb>()
select t).ToList().First();
}
else
{
var leastAnsweredId = grouped
.OrderBy(x => x.Count)
.Select(x => x.QuestionId)
.FirstOrDefault();
nextQuestion = (from t in m_dbSession.Query<QuestionDb>()
where t.Id == leastAnsweredId
select t).ToList().FirstOrDefault();
}
}
所以我得到了一个流畅的 nHibernate table 像这样:
public class AnsweredQuestionDb
{
public virtual long Id { get; set; }
public virtual QuestionDb Question { get; set; }
public virtual AnswerDb Answer { get; set; }
}
QuestionDb 和 AnswerDb 是其他流利的 nHibernate tables.
所以我想 select 回答最少的问题。因此,如果问题 A 被回答了 3 次,问题 B、C 和 D 回答了 4 次,结果应该是问题 A。
这就是我实际工作的重点:
var leastAnswerdQuestion = (from t in m_dbSession.Query<AnsweredQuestionDb>()
group t.Id by t.Question into groups
select groups.ToList().Sum()).ToList()[0];
但是这个抛出异常,里面没有or order by。这甚至可以在一次查询中完成吗?
我希望清楚我想做什么。
我假设在 sql 中看起来像这样:
SELECT Question_id, count(Question_id)
FROM AnsweredQuestionDb
GROUP BY Question_id
ORDER By count(Question_id)
所以将 LINQ 查询写成 SQL:
var grouped =
from t in m_dbSession.Query<AnsweredQuestionDb>()
group t by t.Question.Id into g
select new
{
QuestionId = g.Key,
Count = g.Count()
};
var leastAnsweredId = grouped
.OrderBy(x => x.Count)
.Select(x => x.QuestionId)
.FirstOrDefault();
因此,在 Svyatoslav Danyliv 的帮助下,我创建了这个:
当然这并不完美,但它是有目的的:
QuestionDb nextQuestion = null;
var grouped = (from t in m_dbSession.Query<AnsweredQuestionDb>()
group t by t.Question.Id into g
orderby g.Count()
select new
{
QuestionId = g.Key,
Count = g.Count()
}).ToList();
var keys = grouped.Select(o => o.QuestionId).ToList();
var notAnswered = (from t in m_dbSession.Query<QuestionDb>()
where
!keys.Contains(t.Id)
select t).ToList().First();
if (notAnswered != null)
{
nextQuestion = notAnswered;
}
else
{
if (grouped.Count() == 0)
{
nextQuestion = (from t in m_dbSession.Query<QuestionDb>()
select t).ToList().First();
}
else
{
var leastAnsweredId = grouped
.OrderBy(x => x.Count)
.Select(x => x.QuestionId)
.FirstOrDefault();
nextQuestion = (from t in m_dbSession.Query<QuestionDb>()
where t.Id == leastAnsweredId
select t).ToList().FirstOrDefault();
}
}