C#7是否允许在linq表达式中解构元组

Ran*_*dom 25 c# linq tuples c#-7.0

我试图在Linq表达式中解构一个元组

// somewhere inside another method
var result = from word in words
             let (original, translation) = Convert(word)
             select original
Run Code Online (Sandbox Code Playgroud)

这是返回元组的方法的签名

(string Original, string Translation) Convert(DictionaryWord word)
{
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

但它不是一个有效的语法.我只能在没有解构的情况下访问元组值:

var result = from word in words
             let result = GetWord(word, mode)
             select result.Original
Run Code Online (Sandbox Code Playgroud)

是否有正确的方法来解构它或Linq表达式中不支持它?

Pat*_*ald 18

似乎没有.

在GitHub上有一个未解决的问题:https://github.com/dotnet/roslyn/issues/6877

编辑

问题转移到dotnet/csharplang#355

  • 到了 2022 年,我们不能使用“from (x, y) in point”或“let (x, y) = point”。 (4认同)
  • 谢谢。遗憾的是它仍然积压 (2认同)

Jul*_*eur 6

C#7.0不支持在Linq查询中进行解构。

只有三种形式的解构使其进入C#7.0(赋值,“ foreach”循环和“ for”循环中的解构)。但是,当语言设计委员会考虑了所有声明变量的可能位置(因此将成为解构的候选对象)并对其进行优先级排序时,“ let”(或可能是“ from”)子句中的解构就成为下一个顺序。

如果您觉得这很有用,请确保在https://github.com/dotnet/csharplang/issues/189上留下笔记或赞许。