您好我的NHibernate DAL中有以下方法:
public IQueryable<WorkCellLoadGraphData> GetWorkCellLoadGraphDataById( string workCellId, DateTime startDate, DateTime endDate )
{
var workCellGraphData = ( from x in this.GetSession().Query<WorkCellLoadGraphData>()
where x.WorkCellId == workCellId && (x.FromTime >= startDate && x.FromTime <= endDate)
select new
{
x.WorkCellId,
x.CalendarId,
x.FromTime,
x.DurationInMinutes
});
return workCellGraphData as IQueryable<WorkCellLoadGraphData>;
}
当我在workCellGraphData上放置断点时,我得到了一个WorkCellGraphData对象的集合。
但是,在RIA域服务类中调用此方法的代码是:
public IQueryable<WorkCellLoadGraphData> GetWorkCellLoadGraphDataById()
{
IQueryable<WorkCellLoadGraphData> result = ManufacturingDao.Instance.GetWorkCellLoadGraphDataById( "13", DateTime.Today, DateTime.Today.AddDays( 14 ) );
return result;
}
始终在“result”中返回null。谁能发现原因?
TIA,
大卫
答案 0 :(得分:1)
在LINQ中,查询的执行通常会推迟到您实际请求数据的那一刻。 在您的第一种方法中,您只是定义了查询,但它从未执行过;在你的第二种方法中,只要你需要列举结果。
答案 1 :(得分:0)
我不确定区别是什么,但我们使用以下方法解决了这个问题:
public IList<WorkCellLoadGraphData> GetWorkCellLoadGraphDataByIdA(string workCellId, DateTime startDate, DateTime endDate)
{
IList<WorkCellLoadGraphData> results = new List<WorkCellLoadGraphData>();
using (var session = this.GetSession())
{
var criteria = session.CreateCriteria(typeof(WorkCellLoadGraphData));
criteria.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property(WorkCellLoadGraphData.WorkCellIdPropertyName), "WorkCellId")
.Add(Projections.Property(WorkCellLoadGraphData.FromTimePropertyName), "FromTime")
.Add(Projections.Property(WorkCellLoadGraphData.DurationPropertyName), "DurationInMinutes")
.Add( Projections.Property( WorkCellLoadGraphData.CalendarIdPropertyName), "CalendarId" )
);
criteria.Add(Restrictions.InsensitiveLike(WorkCellLoadGraphData.WorkCellIdPropertyName, workCellId));
criteria.Add(Restrictions.Between(WorkCellLoadGraphData.FromTimePropertyName, startDate, endDate));
criteria.SetResultTransformer(new AliasToBeanResultTransformer(typeof(WorkCellLoadGraphData)));
results = criteria.List<WorkCellLoadGraphData>();
}
return results;
}
至少可以说这真是令人费解......