【发布时间】:2012-03-22 10:39:34
【问题描述】:
我有一个奇怪的错误,我不明白,将 LINQ 的 IEnumerable 更改为中途修复它,我不明白为什么
不是真正的代码,但非常相似 下面的代码不起作用:
// an IEnumerable of some object (Clasess) internally an array
var ansestors = GetAnsestors();
var current = GetCurrentServerNode();
var result = from serverNode in ansestors
select new PolicyResult
{
//Some irrelevant stuff
OnNotAvailableNode = NodeProcessingActionEnum.ContinueExecution,
};
var thisNode = new PolicyResult
{
//Some irrelevant stuff
OnNotAvailableNode = NodeProcessingActionEnum.ThrowException,
};
result = result.Reverse();
result = result.Concat(new List<PolicyResult> { thisNode });
result.First().OnNotAvailableNode = NodeProcessingActionEnum.ThrowException;
// When looking in the debugger, and in logs, the first element of the
// result sequence has OnNotAvailableNode set to ContinueExecution
// Which doesnt make any sense...
但是当我将结尾更改为以下内容时,它会起作用:
result = result.Reverse();
result = result.Concat(new List<PolicyResult> { thisNode });
var policyResults = result.ToList();
var firstPolicyResult = policyResults.First();
firstPolicyResult.OnNotAvailableNode = NodeProcessingActionEnum.ThrowException;
return policyResults;
这里的所有类型都是类(引用类型),除了 NodeProcessingActionEnum 是一个枚举。
这是一个错误吗?
我错过了一些关于 LINQ 的重要内容?
帮助?
【问题讨论】:
-
Reverse()不返回 void 吗?你没有收到错误cannot convert void to ..... -
@V4Vendetta msdn.microsoft.com/en-us/library/bb358497.aspx
-
他说代码不行……
-
@HenkHolterman 该行是从代码中复制粘贴的。 Enumerable.Reverse() 返回一个新的反向集合,它是原始集合的副本...
-
@AK_ 哎呀,谢谢,我还以为某处有
ToList。