[英]Does C# 7 allow to deconstruct tuples in linq expressions
我正在嘗試在 Linq 表達式中解構一個元組
// somewhere inside another method
var result = from word in words
let (original, translation) = Convert(word)
select original
這是返回元組的方法的簽名
(string Original, string Translation) Convert(DictionaryWord word)
{
// implementation
}
但這不是有效的語法。 我只能在不解構的情況下訪問元組值:
var result = from word in words
let result = GetWord(word, mode)
select result.Original
是否有適當的方法來解構它,或者它在 Linq 表達式中不受支持?
C# 7.0 不支持 Linq 查詢中的解構。
只有三種解構形式進入了 C# 7.0(在賦值、“foreach”循環和“for”循環中解構)。 但是,當語言設計委員會考慮了所有可能的聲明變量的位置(因此將成為解構的候選者)並確定它們的優先級時,“let”(可能還有“from”)子句中的解構是下一個。
如果您覺得這對您有用,請務必在https://github.com/dotnet/csharplang/issues/189上留言或點贊。
你可以這樣做:
public static (string Original, string Translation) Convert(string word)
{
return ("Hello", "Hello translated");
}
static void Main(string[] args)
{
List<string> words = new List<string>();
words.Add("Hello");
var result = from word in words
select Convert(word).Translation;
Console.WriteLine("Hello, world!" + result.FirstOrDefault());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.