最近我遇到了这样的问题:
What numbers will be printed considering the following code:
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 3, 5, 7, 9 };
int threshold = 6;
var query = from value in numbers where value >= threshold select value;
threshold = 3;
var result = query.ToList();
result.ForEach(Console.WriteLine);
Console.ReadLine();
}
}
答案:3, 5, 7, 9
这对我来说非常令人惊讶。我认为threshold
值将在查询构造中被放入堆栈,稍后在执行时,该数字将被拉回并在条件中使用..这种情况并没有发生。
另一种情况(numbers
在执行之前设置为null
:
static void Main(string[] args)
{
int[] numbers = { 1, 3, 5, 7, 9 };
int threshold = 6;
var query = from value in numbers where value >= threshold select value;
threshold = 3;
numbers = null;
var result = query.ToList();
...
}
似乎对查询没有影响。它打印出的答案与前面的例子完全相同。
有谁能帮我理解幕后真的发生了什么?在更改threshold
时,为什么更改numbers
会对查询执行产生影响?
答案 0 :(得分:26)
您的查询可以用方法语法编写:
var query = numbers.Where(value => value >= threshold);
或者:
Func<int, bool> predicate = delegate(value) {
return value >= threshold;
}
IEnumerable<int> query = numbers.Where(predicate);
这些代码片段(包括您在查询语法中的查询)都是等效的。
当你展开这样的查询时,你会发现predicate
是anonymous method,而threshold
是该方法中的closure。这意味着它将在执行时采用该值。编译器将生成一个实际(非匿名)方法来处理它。声明时不会执行该方法,但枚举query
时执行的每个项目(执行 deferred )。由于枚举发生在threshold
的值更改(并且threshold
是闭包)之后,因此使用新值。
将numbers
设置为null
时,将引用设置为无处,但对象仍然存在。 IEnumerable
返回的Where
(并在query
中引用)仍然引用它,现在初始引用为null
无关紧要。
这解释了行为:numbers
和threshold
在延迟执行中扮演不同的角色。 numbers
是对枚举数组的引用,而threshold
是局部变量,其范围被“转发”到匿名方法。
扩展,第1部分:在枚举期间修改闭包
当您更换线路时,您可以更进一步...
var result = query.ToList();
...与:
List<int> result = new List<int>();
foreach(int value in query) {
threshold = 8;
result.Add(value);
}
您正在做的是在数组迭代期间更改threshold
的值。当您第一次点击循环体时(value
为3时),您将阈值更改为8,这意味着将跳过值5和7,并且要添加到列表中的下一个值是9.原因是在每次迭代时将再次评估threshold
的值,并且将使用然后有效值。并且由于阈值已变为8,因此数字5和7不再评估为更大或相等。
扩展,第2部分:实体框架不同
为了使事情变得更复杂,当您使用LINQ提供程序创建与原始查询不同的查询然后执行它时,情况会略有不同。最常见的例子是实体框架(EF)和LINQ2SQL(现在很大程度上被EF取代)。这些提供程序在枚举之前从原始查询创建SQL查询。从那时起,闭包的值只被评估一次(它实际上不是闭包,因为编译器生成表达式树而不是匿名方法),枚举期间threshold
中的更改没有效果在结果。在将查询提交到数据库之后会发生这些更改。
从中得到的教训是,您必须始终了解您正在使用的LINQ的哪种风格,并且对其内部工作的某些理解是一个优势。
答案 1 :(得分:2)
最简单的方法是查看编译器将生成什么。您可以使用此网站:https://sharplab.io
using System.Linq;
public class MyClass
{
public void MyMethod()
{
int[] numbers = { 1, 3, 5, 7, 9 };
int threshold = 6;
var query = from value in numbers where value >= threshold select value;
threshold = 3;
numbers = null;
var result = query.ToList();
}
}
这是输出:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[module: UnverifiableCode]
public class MyClass
{
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int threshold;
internal bool <MyMethod>b__0(int value)
{
return value >= this.threshold;
}
}
public void MyMethod()
{
MyClass.<>c__DisplayClass0_0 <>c__DisplayClass0_ = new MyClass.<>c__DisplayClass0_0();
int[] expr_0D = new int[5];
RuntimeHelpers.InitializeArray(expr_0D, fieldof(<PrivateImplementationDetails>.D603F5B3D40E40D770E3887027E5A6617058C433).FieldHandle);
int[] source = expr_0D;
<>c__DisplayClass0_.threshold = 6;
IEnumerable<int> source2 = source.Where(new Func<int, bool>(<>c__DisplayClass0_.<MyMethod>b__0));
<>c__DisplayClass0_.threshold = 3;
List<int> list = source2.ToList<int>();
}
}
[CompilerGenerated]
internal sealed class <PrivateImplementationDetails>
{
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 20)]
private struct __StaticArrayInitTypeSize=20
{
}
internal static readonly <PrivateImplementationDetails>.__StaticArrayInitTypeSize=20 D603F5B3D40E40D770E3887027E5A6617058C433 = bytearray(1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 7, 0, 0, 0, 9, 0, 0, 0);
}
如您所见,如果您更改threshold
变量,则确实会更改auto-generated
类中的字段。因为您可以随时执行查询,所以不可能引用堆栈中的字段 - 因为当您退出方法时,threshold
将从堆栈中删除 - 因此编译器会将此字段更改为auto-生成具有相同类型的field
的类。
第二个问题:为什么null工作(在此代码中不可见)
当您使用:source.Where
时,它会调用此扩展方法:
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
if (source is Iterator<TSource>) return ((Iterator<TSource>)source).Where(predicate);
if (source is TSource[]) return new WhereArrayIterator<TSource>((TSource[])source, predicate);
if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate);
return new WhereEnumerableIterator<TSource>(source, predicate);
}
如您所见,它将引用传递给:
WhereEnumerableIterator<TSource>(source, predicate);
以下是where iterator
的源代码:
class WhereEnumerableIterator<TSource> : Iterator<TSource>
{
IEnumerable<TSource> source;
Func<TSource, bool> predicate;
IEnumerator<TSource> enumerator;
public WhereEnumerableIterator(IEnumerable<TSource> source, Func<TSource, bool> predicate) {
this.source = source;
this.predicate = predicate;
}
public override Iterator<TSource> Clone() {
return new WhereEnumerableIterator<TSource>(source, predicate);
}
public override void Dispose() {
if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose();
enumerator = null;
base.Dispose();
}
public override bool MoveNext() {
switch (state) {
case 1:
enumerator = source.GetEnumerator();
state = 2;
goto case 2;
case 2:
while (enumerator.MoveNext()) {
TSource item = enumerator.Current;
if (predicate(item)) {
current = item;
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector) {
return new WhereSelectEnumerableIterator<TSource, TResult>(source, predicate, selector);
}
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
return new WhereEnumerableIterator<TSource>(source, CombinePredicates(this.predicate, predicate));
}
}
所以它只是简单地在私有字段中引用我们的源对象。
答案 2 :(得分:0)
变量&#34;数字&#34;是查询已被实例化并在其上工作的那个。它保留了设置查询时的值。那个&#34;阈值&#34;在执行查询时,在谓词中使用valiable,它位于ToList()中。此时,谓词在trashhold上找到了值。
无论如何,这不是一个明确的代码......
答案 3 :(得分:0)
我认为理解它的最简单方法就是每行查看一行并考虑执行什么和何时执行,而不是只在内存中声明。
//this line declares numbers array
int[] numbers = { 1, 3, 5, 7, 9 };
//that one declares value of threshold and sets it to 6
int threshold = 6;
//that line declares the query which is not of the type int[] but probably IQueryable<int>, but never executes it at this point
//To create IQueryable it still iterates through numbers variable, and kind of assign lambda function to each of the items.
var query = from value in numbers where value >= threshold select value;
//that line changes threshold value to 6
threshold = 3;
//that line executes the query defined easier, and uses current value value of threshold, as it is only reference
var result = query.ToList();
result.ForEach(Console.WriteLine);
Console.ReadLine();
该机制为您提供了一些很好的功能,例如在多个位置构建查询,并在每次运行准备就绪后执行它。
将numbers
变量设置为null
不会更改立即调用的结果,以进行枚举。
答案 4 :(得分:0)
您的LINQ查询不会返回请求的数据,它会返回获取可以逐个访问数据元素的内容的可能性。
在软件方面:您的LINQ语句的值是IEnumerable<T>
(或IQueryable<T>
,此处不再进一步讨论)。此对象不包含您的数据。
事实上,你不能用IEnumerable<T>
做很多事情。它唯一能做的就是生成另一个实现IEnumerator<T>
的对象。 (注意区别:IEnumerable vs IEnumerator)。这个`GetEnumerator()&#39;功能是&#34; 得到可以访问的东西...... &#34;在我的第一句话中。
您从IEnumerable<T>.GetEnumerator()
获得的对象实现了IEnumerator。此对象也不必保存您的数据。它只知道如何生成数据的第一个元素(如果有的话),如果它有一个元素,它知道如何获取下一个元素(如果有的话)。这是&#34; 可以逐个访问数据元素&#34;从我的第一句话开始。
因此,IEnumerable<T>
和Enumerator<T>
都不会(必须)保存您的数据。它们只是帮助您按照定义的顺序访问数据的对象。
在早期,当我们没有List<T>
或类似的实施IEnumerable<T>
的集合类时,实现IEnumerable<T>
和{{1}是非常麻烦的函数IEnumerator<T>
,Reset
和Current
。实际上,现在很难找到实现MoveNext
的示例,这些示例不使用也实现IEnumerator<T>
的类。 Example
关键字IEnumerator<T>
的引入大大简化了Yield
和IEnumerable<T>
的实施。如果某个函数包含IEnumerator<T>
,则会返回Yield return
:
IEnumerable<T>
请注意,我使用术语序列。它不是List而不是Dictionary,你只能通过询问第一个元素来访问元素,并反复询问下一个元素。
您可以使用IEnumerable<double> GetMySpecialNumbers()
{ // returns the sequence: 0, 1, pi and e
yield return 0.0;
yield return 1.0;
yield return 4.0 * Math.Atan(1.0);
yield return Math.Log(1.0)
}
和IEnumerable<T>.GetEnumerator()
的三个函数来访问序列的元素。这种方法很少再使用:
IEnumerator<T>
随着IEnumerable<double> myNumbers = GetMySpecialNumbers();
IEnumerator<double> enumerator = myNumbers.GetEnumerator();
enumerator.Reset();
// while there are numbers, write the next one
while(enumerator.MoveNext())
{ // there is still an element in the sequence
double valueToWrite = enumerator.Current();
Console.WriteLine(valueToWrite);
}
的引入,这变得更加容易:
foreach
在内部,这将执行foreach (double valueToWrite in GetMySpecialNumbers())
Console.WriteLine(valueToWrite);
和GetNumerator()
/ Reset()
/ MoveNext()
所有通用集合类(如List,Array,Dictionary,HashTable等)都实现IEnumerable。大多数情况下,函数返回一个IEnumerable,你会在内部发现它使用其中一个集合类。
Current()
和yield
之后的另一项伟大发明是引入扩展方法。见extension methods demystified。
通过扩展方法,您只需使用您有权访问的功能,即可获取一个您无法更改的课程,例如foreach
并为其编写新功能。
这是LINQ的推动力。它使我们能够为所有内容编写新功能:&#34;嘿,我是一个序列,你可以要求我的第一个元素和我的下一个元素&#34; (=我实现IEnumerable)。
如果查看source code of LINQ,,您会发现LINQ函数(如Where / Select / First / Reverse / ...等)被写为IEnumerable的扩展函数。他们中的大多数使用泛型集合类(HashTable,Dictionary),其中一些使用yield return,有时你甚至会看到基本的IEnumerator函数,如Reset / MoveNext
通常,您可以通过连接LINQ函数来编写新功能。但是,请记住,有时候List<T>
会使您的函数更容易理解,从而更容易重用,调试和维护。
示例:假设您有一系列生成的yield
。每个Products
都有Product
属性DateTime
,表示产品的生产完成时间。
假设您想知道两个完成的产品之间有多少时间。 问题:无法计算第一个产品。
收益率很容易:
ProductCompletedTime
尝试在Linq中执行此操作,要了解会发生什么将会更加困难。
<强>结论强> IEnumerable不保存您的数据,它只保留逐个访问您数据的潜力。
访问数据最常用的方法是foreach,ToList(),ToDictionary,First等。
每当你需要编写一个返回困难public static IEnumerable<TimeSpan> ToProductionTimes<Product>
(this IEnumerable<Product> products)
{
var orderedProducts = product.OrderBy(product => product.ProductionTime;
Product previousProduct = orderedProducts.FirstOrDefault();
foreach (Product product in orderedProducts.Skip(1))
{
yield return product.ProductCompletedTime - previouseProduct.ProductCompletedTime;
previousProduct = product;
}
}
的函数时,至少考虑编写一个IEnumerable<T>
函数。