gpt4 book ai didi

c# - 为什么此 Linq 代码总是抛出 System.StackOverflowException?

转载 作者:行者123 更新时间:2023-11-30 22:49:27 25 4
gpt4 key购买 nike

 //A query to a local object
var deletionCommands = commands
.Where(a => a.Operation != Operation.Addition)
.Select(a => new { a.Prestador.cod_prestador, a.Prestador.cod_desdobramento })
;
//A Linq-To-SQL query
var toDelete = db.Prestadors
.Where(a => deletionCommands.Contains(new { a.cod_prestador, a.cod_desdobramento }))
;
db.Prestadors.DeleteAllOnSubmit(toDelete);
db.SubmitChanges();

唯一解决问题的是一个显式循环:

 foreach (var command in commands)
{
if(command.Operation != Operation.Addition)
{
var toDelete = db.Prestadors
.Where(a =>
a.cod_prestador == command.Prestador.cod_prestador &&
a.cod_desdobramento == command.Prestador.cod_desdobramento
);
db.Prestadors.DeleteAllOnSubmit(toDelete);
}
}
db.SubmitChanges();

最佳答案

这确实是一个错误,并在 LINQ 4.0 中得到纠正

http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40

Query stability Contains now detects self-referencing IQueryable and doesn't cause a stack overflow

编辑 在 .NET 3.5 中解决问题:当使用“自动生成的值”= True 时,您必须将“延迟加载”设置为 False - 否则会出现递归错误。

EDIT2 上述解决方案无效。

关于c# - 为什么此 Linq 代码总是抛出 System.StackOverflowException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1119678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com