我有两个表:Parent和Child,它们有以下关系:Parent有许多children .
public class Parent
{
public DateTime Timestamp;
public IList<Child> Child;
}
public Child
{
public string Name;
}
我想选择父和子,按时间戳排序,只得到索引x到y之间的行。
public IList<Parent> Get(DateTime from, DateTime to, int startRow, int count)
{
QueryOver<Parent>().Where(row => row.Timestamp >= from)
.And(row => row.Timestamp <= to).OrderBy(row => row.Timestamp).Asc.List();
}
我不知道如何只得到所需的行。
我应该用QueryOver吗?还是用HQL更好?
谢谢
我改变了关系,我只用一个表代替了Parent和Child:
public class Info
{
public DateTime Timestamp;
public string Name;
}
为了获得日期之间的所有记录,排序并从索引startRow到startRow +计数,我使用了以下命令:
public IList<Info> GetInfo (DateTime fromDate, DateTime toDate, int startRow, int count)
{
IList<Info> result =
QueryOver<Info>()
.Where(row => row.Timestamp >= fromDate)
.And(row => row.Timestamp <= toDate)
.OrderBy(row => row.Timestamp).Asc
.Skip(startRow).Take(count).List();
return result;
}
结果SQL为:
SELECT * FROM Info WHERE timestamp >= :fromDate AND timestamp <= :toDate
ORDER BY timestamp ASC OFFSET :startRow ROWS FETCH NEXT :count ROWS ONLY