我一直在浏览Linqpad示例 linq 示例。在“Projecting - SelectMany”下,他们要求您尝试将以下 linq 语法翻译成 Fluent 语法。这是那里的样本:
var fullNames = new[] { "Anne Williams", "John Fred Smith", "Sue Green" }
.AsQueryable();
IEnumerable<string> query =
from fullName in fullNames
from name in fullName.Split()
orderby fullName, name
select name + " came from " + fullName;
query.Dump();
我使用以下方法对其进行了转换:
var myQuery = fullNames
.SelectMany(
fullName => fullName.Split()
.OrderBy(fn => fullName)
.ThenBy(fn=> fn)
, (fullName, name) => ((name + " came from ") + fullName)
);
但是 linqpad 生成以下内容:
IEnumerable<string> query2 = fullNames
.SelectMany (fName => fName.Split().Select (name => new { name, fName } ))
.OrderBy (x => x.fName)
.ThenBy (x => x.name)
.Select (x => x.name + " came from " + x.fName);
两者都返回相同的结果。一个比另一个快吗?你觉得哪一个更好看?想法?