21

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

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();

它成功运行一次,但在第二次(或第三次、第四次等)执行时,它会抛出一个无效的强制转换异常,例如:

致命错误:System.InvalidCastException:无法将类型 'System.Linq.EnumerableQuery`1[<>f__AnonymousType0`3[System.Int64,System.DateTime,Team]]' 转换为 'System.Collections.Generic.IEnumerable`1[< >f__AnonymousType0`3[System.Int64,System.DateTime,Team]]'。在 NHibernate.Linq.DefaultQueryProvider.Execute[TResult](表达式表达式)

堆栈跟踪的其余部分因勇敢而被压制。

查询总是在出错之前在数据库中执行。它不返回任何记录,但没关系。如果我重建解决方案并再次运行,查询将在第一次再次执行,然后在我运行它时开始互相抛出异常。其他查询每次运行都没有任何问题。我不知道是什么导致了错误。

重要的是要说这段代码是在CSharpCodeProvider环境中运行的,但我不知道它是否能有所作为。

更新

即使使用最简单的查询形式也会发生这种情况:

var employeeTeam = Session.Query<EmployeeTeam>()
                       .Select(x => new
                       {
                           x.Id
                       }).ToList();

仅第一次运行正常。但是,如果我将 annon 对象从 更改{ x.Id }{ x.TeamId },例如,它在第一次运行正常,那么异常再次发生。

更新 2

我只是意识到,如果我将以下属性添加到 annon 对象,则查询每次都有效:

Rnd = (new Random().Next(1, 999))

那么,可能是缓存问题?

更新 3

我将 NHibernate 从3.3to更新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();

错误信息:

GenericADOException:值“{ HoliDayCities = System.Collections.Generic.List`1[System.Int64], HoliDayStates = System.Collections.Generic.List`1[System.Int64], Date = 01/02/2015 00:00 :00 }" 不是 "<>f__AnonymousType1`3[System.Collections.Generic.List`1[System.Int64],System.Collections.Generic.List`1[System.Int64],System.DateTime]" 并且不能用于这个集合。参数名称:值

如果我像前面提到的那样Rnd()在作用域上添加函数,它就可以正常工作。Select该问题仅发生在匿名对象上。

4

2 回答 2

1

看起来这是操纵匿名类型和 NHibernate 的问题。我强烈建议返回一个简单的结果集,使用 ToList() 实现结果集,然后对该结果集进行投影。

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

var projectedTeam = employeeTeam.Select(x => new {x.Id});
于 2016-02-05T19:43:52.117 回答
-2

我不认为 N​​hib LINQ 提供程序支持将投影作为延迟执行的一部分。我认为您需要将投影放在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
                   });
于 2016-01-30T18:58:10.730 回答