如何将linq查询转换为方法调用

时间:2015-07-30 17:10:45

标签: c# linq sprache

我尝试使用Sprache制作解析器 我正在使用F#。我见过的所有库的例子都使用了Linq查询语法。我尝试使用"查询"将linq翻译为f#。计算表达式,但我迷失了。

以下是我一直在使用的示例。

[TestClass]
public class tests
{
    [TestMethod]
    public void test()
    {
        var input = "\"this is text\"";
        var content = Class1.Quoted.Parse(input);
        Assert.AreEqual("this is text", content);
    }
}
public class Class1
{
    public static readonly Parser<string> Quoted =
        (from open in Parse.Char('"')
         from content in Parse.CharExcept('"').Many().Text()
         from close in Parse.Char('"')
         select content);

    public static readonly Parser<string> Quoted2 =
        Parse.Char('"').SelectMany(open => 
        Parse.CharExcept('"').Many().Text(), ())
}

显而易见的方法是将linq转换为方法调用,但我不知道如何做到这一点。我试着看看ILSpy,但这让我有些困惑。

Linq查询如何看作直接方法调用?

1 个答案:

答案 0 :(得分:0)

翻译你的例子

ObjectCache

给出

(from open in Parse.Char('"')
from content in Parse.CharExcept('"').Many().Text()
from close in Parse.Char('"')
select content);

注意:对F#采用更惯用的方法可能会很棘手,因为计算表达式似乎没有等于C#LINQ语法预编译到的SelectMany的重载(并且Sprache提供)。