gpt4 book ai didi

.net - 为什么在枚举结果之前获取 Count() 时得到 "The result of a query cannot be enumerated more than once"?

转载 作者:行者123 更新时间:2023-12-03 18:41:06 25 4
gpt4 key购买 nike

这是我的代码。为便于阅读而简化

var query = from p in people select p;

// here is the point that probably causes the issue
ObjectResult<int> idsThatMatch = getIdsThatMatchFullTextSearch("andre");
query = from p in query where idsThatMatch.Contains(p.id) select p;

var count = query.Count();
query = query.OrderBy(p => p.id);
var pessoas = query.Skip(90).Take(30).ToList();

我需要在 skip/take 之前读取计数以获取分页之前的记录总数。计数工作正常。但是在我摘录的最后一行它触发了异常

The result of a query cannot be enumerated more than once

为什么?顺便说一句,伯爵不应该列举任何东西。我该如何解决?谢谢

编辑

人们以为我在使用存储过程,但我不是。实际上我正在使用“选择”。代码在评论下方。

编辑 2

我只是在没有“select in”部分的情况下测试了上面的代码,它工作正常

编辑 3

使用此行有效:

ObjectResult<int> idsThatMatch = (getIdsThatMatchFullTextSearch("andre");
query = from p in query where idsThatMatch.Contains(p.id) select p).ToArray();

谢谢

最佳答案

问题是这一行:

ObjectResult<int> idsThatMatch = getIdsThatMatchFullTextSearch("andre");

这返回 ObjectResult 而不是 ObjectQueryIQueryable。结果只能迭代一次。一旦您使用 Count 执行第一个查询,您就不能再使用结果,并且您的第二个查询执行将失败,因为它将再次尝试迭代结果。

ObjectResult 不在数据库端处理 - 它是执行 ObjectQuery 的结果,一次执行只能有一个结果集枚举。它就像游标一样在您的应用程序中遍历结果集。

关于.net - 为什么在枚举结果之前获取 Count() 时得到 "The result of a query cannot be enumerated more than once"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587901/

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