LINQ:将.Single()转换为查询表示法

Red*_*ces 2 c# linq

所以我知道查询符号

var word = from s in stringList
           where s.Length == 3
           select s;
Run Code Online (Sandbox Code Playgroud)

相当于点符号

var word = stringList
           .Where(s => s.Length == 3)
           .Select(s => s);
Run Code Online (Sandbox Code Playgroud)

但是,如何将此点符号转换为查询符号?

var word = wordsList
           .Single(p => p.Id == savedId);
Run Code Online (Sandbox Code Playgroud)

我在Google上找不到太多资源.

dee*_*see 6

你不能.查询语法中不能使用很多LINQ函数.充其量,你可以将两者结合起来并做类似的事情

var word = (from p in wordsList
            where p.Id == savedId
            select p).Single()
Run Code Online (Sandbox Code Playgroud)

但在简单的情况下collection.Single(condition),"点符号"似乎对我来说更具可读性.

LINQ在MSDN上使用了一个关键字列表,您可以看到哪些函数已从该列表中集成到该语言中.

  • @Vache ......这正是OP要求转换的内容. (2认同)