在C#中理解LINQ中的延迟评估

时间:2018-03-28 06:34:29

标签: c# linq lazy-evaluation

我正在阅读有关LINQ的this article,并且无法理解在延迟评估方面如何执行查询。

因此,我将文章中的示例简化为此代码:

void Main()
{
    var data =
        from f in GetFirstSequence().LogQuery("GetFirstSequence")
        from s in GetSecondSequence().LogQuery("GetSecondSequence", f)
        select $"{f} {s}";

    data.Dump(); // I use LINQPAD to output the data
}

static IEnumerable<string> GetFirstSequence()
{
    yield return "a";
    yield return "b";
    yield return "c";
}

static IEnumerable<string> GetSecondSequence()
{
    yield return "1";
    yield return "2";
}

public static class Extensions
{
    private const string path = @"C:\dist\debug.log";

    public static IEnumerable<string> LogQuery(this IEnumerable<string> sequence, string tag, string element = null)
    {
        using (var writer = File.AppendText(path)) 
        {
            writer.WriteLine($"Executing query {tag} {element}");
        }
        return sequence;
    }
}

执行此代码后,我在debug.log文件中有以下内容:

  • 执行查询GetFirstSequence
  • 执行查询GetSecondSequence a
  • 执行查询GetSecondSequence b
  • 执行查询GetSecondSequence c

可以从逻辑上解释。

当我想将前三个元素与前三个元素交错时,事情变得奇怪了:

void Main()
{
    var data =
        from f in GetFirstSequence().LogQuery("GetFirstSequence")
        from s in GetSecondSequence().LogQuery("GetSecondSequence", f)
        select $"{f} {s}";

    var shuffle = data;
    shuffle = shuffle.Take(3).LogQuery("Take")
        .Interleave(shuffle.Skip(3).LogQuery("Skip")).LogQuery("Interleave");

    shuffle.Dump();
}

当然我需要添加扩展方法来交错两个序列(来自上面提到的文章):

public static IEnumerable<string> Interleave(this IEnumerable<string> first, IEnumerable<string> second)
    {
        var firstIter = first.GetEnumerator();
        var secondIter = second.GetEnumerator();

        while (firstIter.MoveNext() && secondIter.MoveNext())
        {
            yield return firstIter.Current;
            yield return secondIter.Current;
        }
    }

执行这些代码后,我在txt文件中得到以下输出:

  • 执行查询GetFirstSequence
  • 执行查询执行
  • 执行查询跳过
  • 执行查询交错
  • 执行查询GetSecondSequence a
  • 执行查询GetSecondSequence a
  • 执行查询GetSecondSequence b
  • 执行查询GetSecondSequence c
  • 执行查询GetSecondSequence b

这让我感到尴尬,因为我不理解我的查询执行的顺序。

为什么以这种方式执行查询?

1 个答案:

答案 0 :(得分:2)

var data =
    from f in GetFirstSequence().LogQuery("GetFirstSequence")
    from s in GetSecondSequence().LogQuery("GetSecondSequence", f)
    select $"{f} {s}";

只是另一种写作方式

var data = GetFirstSequence()
    .LogQuery("GetFirstSequence")
    .SelectMany(f => GetSecondSequence().LogQuery("GetSecondSequence", f), (f, s) => $"{f} {s}");

让我们逐步完成代码:

var data = GetFirstSequence() // returns an IEnumerable<string> without evaluating it
    .LogQuery("GetFirstSequence") // writes "GetFirstSequence" and returns the IEnumerable<string> from its this-parameter without evaluating it
    .SelectMany(f => GetSecondSequence().LogQuery("GetSecondSequence", f), (f, s) => $"{f} {s}"); // returns an IEnumerable<string> without evaluating it

var shuffle = data;
shuffle = shuffle
    .Take(3) // returns an IEnumerable<string> without evaluating it
    .LogQuery("Take") // writes "Take" and returns the IEnumerable<string> from its this-parameter without evaluating it
    .Interleave(
        shuffle
            .Skip(3) // returns an IEnumerable<string> without evaluating it
            .LogQuery("Skip") // writes "Skip" and returns the IEnumerable<string> from its this-parameter without evaluating it
    ) // returns an IEnumerable<string> without evaluating it
    .LogQuery("Interleave"); // writes "Interleave" and returns the IEnumerable<string> from its this-parameter without evaluating it

到目前为止,代码负责前四行输出:

Executing query GetFirstSequence
Executing query Take
Executing query Skip
Executing query Interleave

没有IEnumerable&lt; string&gt;已被评估过。

最后,shuffle.Dump()遍历shuffle,从而评估IEnumebles。

data进行迭代打印以下内容,因为SelectMany()GetSecondSequence()中的每个元素调用了LogQuery()GetFirstSequence()

Executing query GetSecondSequence a
Executing query GetSecondSequence b
Executing query GetSecondSequence c

迭代shuffle与迭代

相同
Interleave(data.Take(3), data.Skip(3))

Interleave()data上的两次迭代中的元素交错,因此也会迭代迭代它们引起的输出。

firstIter.MoveNext();
// writes "Executing query GetSecondSequence a"
secondIter.MoveNext();
// writes "Executing query GetSecondSequence a"
// skips "a 1" from second sequence
// skips "a 2" from second sequence
// writes "Executing query GetSecondSequence b"
// skips "b 1" from second sequence
yield return firstIter.Current; // "a 1"
yield return secondIter.Current; // "b 2"
firstIter.MoveNext();
secondIter.MoveNext();
// writes "Executing query GetSecondSequence c"
yield return firstIter.Current; // "a 2"
yield return secondIter.Current; // "c 1"
firstIter.MoveNext();
// writes "Executing query GetSecondSequence b"
secondIter.MoveNext();
yield return firstIter.Current; // "b 1"
yield return secondIter.Current; // "c 2"