我一直在试验 Linq,看看它能做什么——到目前为止我真的很喜欢它 :)
我为算法编写了一些查询,但没有得到预期的结果......枚举总是返回空:
情况1
List<some_object> next = new List<some_object>();
some_object current = null;
var valid_next_links =
from candidate in next
where (current.toTime + TimeSpan.FromMinutes(5) <= candidate.fromTime)
orderby candidate.fromTime
select candidate;
current = something;
next = some_list_of_things;
foreach (some_object l in valid_next_links)
{
//do stuff with l
}
我将查询声明更改为像这样内联,并且效果很好:
案例#2
foreach (some_object l in
(from candidate in next
where (current.toTime + TimeSpan.FromMinutes(5) <= candidate.fromTime)
orderby candidate.fromTime
select candidate))
{
//do stuff with l
}
有谁知道为什么它在 #1 的情况下不起作用?按照我的理解,当你声明它时没有评估查询,所以我看不出有什么区别。