Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用本地值延迟执行Linq_C#_.net_Linq_List - Fatal编程技术网

C# 使用本地值延迟执行Linq

C# 使用本地值延迟执行Linq,c#,.net,linq,list,C#,.net,Linq,List,我一直在试验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 + Time

我一直在试验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
}
List next=new List();
某些对象当前为空;
var有效\u下一个\u链接=
来自下一位候选人

其中(current.toTime+TimeSpan.FromMinutes(5)对
current
的更改将被捕获,但查询已经知道
next
的值。向现有列表中添加额外的项将使它们显示在查询中,但将变量的值更改为完全引用不同的列表不会产生任何效果。基本上,如果您从查询扩展查询,则将表达式转换为“正常”表单中,lambda表达式中存在的任何变量都将被捕获为变量,但直接作为参数存在的任何变量都将立即求值。这将只捕获变量的引用值,而不捕获列表中存在的项,但这仍然意味着不会看到更改变量值本身。您的第一个查询ds至:

var valid_next_links = next
      .Where(candidate => (current.toTime + TimeSpan.FromMinutes(5) <= candidate.fromTime))
      .OrderBy(candidate => candidate.fromTime);
var valid\u next\u links=next
.Where(candidate=>(current.toTime+TimeSpan.FromMinutes(5)candidate.fromTime);
var valid_next_links = next
      .Where(candidate => (current.toTime + TimeSpan.FromMinutes(5) <= candidate.fromTime))
      .OrderBy(candidate => candidate.fromTime);