Don*_*own 21 c# linq nhibernate csharpcodeprovider
我有一个简单的查询如下:
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();
Run Code Online (Sandbox Code Playgroud)
它成功运行一次,但是当第二次执行时(或者第三次,第四次等),它会抛出一个无效的强制转换异常,例如:
致命错误: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的,小组]]".在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式表达式)
其余的筹码追踪表现得非常勇敢.
查询始终在错误之前在数据库中执行.它不返回任何记录,但它没问题.如果我重新构建解决方案并再次运行,则第一次执行查询,然后在我运行它时开始抛出异常.其他查询每次运行都没有任何问题.我不知道导致错误的原因.
重要的是说这段代码是在CSharpCodeProvider环境中运行的,但我不知道它是否会有所作为.
UPDATE
即使使用最简单的查询形式,它也会发生:
var employeeTeam = Session.Query<EmployeeTeam>()
.Select(x => new
{
x.Id
}).ToList();
Run Code Online (Sandbox Code Playgroud)
它只是第一次运行正常.但是,如果我将annon对象更改{ x.Id }为{ x.TeamId },例如,它在第一次运行正常,则异常再次发生.
更新2
我只是意识到,如果我将以下属性添加到annon对象,则查询每次都有效:
Rnd = (new Random().Next(1, 999))
Run Code Online (Sandbox Code Playgroud)
那么,缓存问题可能呢?
更新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();
Run Code Online (Sandbox Code Playgroud)
错误信息:
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]"而不能用于此系列.参数名称:value
如果我像前面提到的那样Rnd()在Select范围上添加函数,它可以正常工作.只有匿名对象才会出现此问题.
Hok*_*ike -2
我认为 Nhib 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
});
Run Code Online (Sandbox Code Playgroud)