Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate查询只运行一次,然后抛出InvalidCastException_C#_Linq_Nhibernate_Csharpcodeprovider - Fatal编程技术网

C# NHibernate查询只运行一次,然后抛出InvalidCastException

C# NHibernate查询只运行一次,然后抛出InvalidCastException,c#,linq,nhibernate,csharpcodeprovider,C#,Linq,Nhibernate,Csharpcodeprovider,我有一个简单的查询,如下所示: var employeeTeam = Session.Query<EmployeeTeam>() .Where(x => x.StartEffective <= competency.FinalDate && // competency.FinalDate is a DateTime employeesIds.

我有一个简单的查询,如下所示:

var employeeTeam = Session.Query<EmployeeTeam>()
                       .Where(x => x.StartEffective <= competency.FinalDate && // competency.FinalDate is a DateTime
                                   employeesIds.Contains(x.EmployeeId)) // employeeIds is a List<long>
                       .OrderByDescending(x => x.StartEffective)
                       .Select(x => new
                       {
                           x.EmployeeId,
                           x.StartEffective,
                           x.Team
                       }).ToList();
那么,可能是缓存问题

更新3

我将NHibernate从
3.3
更新为
4.0.0.4
,它几乎解决了除一个查询之外的所有问题:

var query = session.Query<Holiday>()
                   .Select(x => new {
                         HoliDayCities = x.City.Select(c => c.Id).ToList(),
                         HoliDayStates = x.State.Select(s => s.Id).ToList(),
    Date = new DateTime((int)(x.Year.HasValue ? x.Year : competencia.InitialDate.Year), (int)x.Month, (int)x.Day)
                   }).ToList();
var query=session.query()
.选择(x=>new{
HoliDayCities=x.City.Select(c=>c.Id).ToList(),
HoliDayStates=x.State.Select(s=>s.Id).ToList(),
日期=新日期时间((int)(x.Year.HasValue?x.Year:Compencia.InitialDate.Year),(int)x.Month,(int)x.Day)
}).ToList();
错误消息:

GenericadException:值“{HoliDayCities”= System.Collections.Generic.List`1[System.Int64],HoliDayStates= System.Collections.Generic.List`1[System.Int64],日期=2015年2月1日 00:00:00}“不是 f__AnonymousType1`3[System.Collections.Generic.List`1[System.Int64],System.Collections.Generic.List`1[System.Int64],System.DateTime] 无法在此集合上使用。参数名称:value


如果我像前面提到的那样在
Select
scope上添加
Rnd()
函数,它可以正常工作。只有匿名对象才会出现问题。

我认为Nhib LINQ提供程序不支持将投影作为延迟执行的一部分。我想你需要把你的投影放在ToList()之后

var employeeTeam=Session.Query()
.其中(x=>x.startefective x.startefective)
托利斯先生()
.选择(x=>new
{
x、 雇员ID,
x、 开始生效,
x、 团队
});

这似乎是操作匿名类型和NHibernate的问题。我强烈建议返回一个简单的结果集,用ToList()具体化结果集,然后对该结果集进行投影

var employeeTeam = Session.Query<EmployeeTeam>()
                          .Select(x => x)
                          .Where(x => x.Id != 0)
                          .ToList();

var projectedTeam = employeeTeam.Select(x => new {x.Id});
var employeeTeam=Session.Query()
.选择(x=>x)
.其中(x=>x.Id!=0)
.ToList();
var projectedTeam=employeeTeam.Select(x=>new{x.Id});

OrderByDescending(x=>x.startefective)在投影后移动到右侧会发生什么?@GertArnold它不会改变结果。即使删除
OrderByDescending
问题仍然存在。@dontvotemdown是否需要它作为列表?特别是,是否有任何原因需要使用:List.Count或运算符[]?如果不是,简单地使用IEnumerable无论如何都是一种更好的做法,因为它可以避免强制立即运行惰性方法,并避免额外的对象分配。@DontVoteMeDown很抱歉回复得太晚。我下载了代码,无法复制异常。我所做的唯一更改是使用windows身份验证登录本地数据库。NHibernate会为查询生成一个查询计划并将其缓存,在以后执行相同的查询时重新使用它。在Linq查询的情况下,首先对它们进行部分求值,以便将内存中的每个可求值子表达式缩减为常量,然后将这些常量提取为参数。然后为得到的部分计算和“参数化”表达式获取(或计算,如果找不到)查询计划。您的问题可能与此查询计划缓存有关。如果你仍然可以复制它,也许你应该使用测试用例。谢谢你的回答。它不会改变结果。我甚至删除了
OrderByDescending
方法,但问题仍然存在。我添加了新信息,您可以将其投影到匿名或命名的对象中。使用投影可确保生成的查询只包含需要检索的值。NHibernate LINQ绝对支持投影,生成的SQL将包含一个基于投影属性的
SELECT
子句。谢谢您的回答。它确实工作得很好。但我还是要解决这个问题,我会努力解决的。这一点很重要,因为它可能在不久的将来发生在我的应用程序的其他地方。
var query = session.Query<Holiday>()
                   .Select(x => new {
                         HoliDayCities = x.City.Select(c => c.Id).ToList(),
                         HoliDayStates = x.State.Select(s => s.Id).ToList(),
    Date = new DateTime((int)(x.Year.HasValue ? x.Year : competencia.InitialDate.Year), (int)x.Month, (int)x.Day)
                   }).ToList();
var employeeTeam = Session.Query<EmployeeTeam>()
                   .Where(x => x.StartEffective <= competency.FinalDate &&    // competency.FinalDate is a DateTime
                               employeesIds.Contains(x.EmployeeId)) //  employeeIds is a List<long>
                   .OrderByDescending(x => x.StartEffective)
                   .ToList()
                   .Select(x => new
                   {
                       x.EmployeeId,
                       x.StartEffective,
                       x.Team
                   });
var employeeTeam = Session.Query<EmployeeTeam>()
                          .Select(x => x)
                          .Where(x => x.Id != 0)
                          .ToList();

var projectedTeam = employeeTeam.Select(x => new {x.Id});