LINQ Let运算符具有lambda语法 [英] LINQ Let operator with lambda syntax
问题描述
我知道linq查询语法中有let运算符.存储子表达式的结果以在后续子句中使用它很有用.
I know that there is let operator in linq query syntax. It is useful to store the result of a sub-expression in order to use it in subsequent clauses.
但是我更喜欢使用linq lambda语法.当我处理大量联接时,我得到对象链(代表联接的每个级别,即:`
But I prefer work with linq lambda syntax. When I work with a lot of joins i get chain of objects (which represent each level of joins ie: `
s.secondJoin.firstJoin.dd.DataType`
我想在变量中设置一些子结果,并将其用于以下更方便的工作:
I would like to set some sub result in variable and use it for more convinient work in the following ie:
let joinResult = s.secondJoin.firstJoin
及之后的
joinResult.dd.DataType
let joinResult = s.secondJoin.firstJoin
and after
joinResult.dd.DataType
问:是否可以使用linq lambda语法?
Q: Is it possible in linq lambda syntax?
推荐答案
在lambda语法中,与let
等效的是Select
,用于创建匿名类型,例如:
Equivalent to let
in lambda syntax is Select
to create an anonymous type, for example:
items.Select(s => new
{
s = s,
joinResult = s.secondJoin.firstJoin
})
此后,您可以像使用let
一样在查询语法中同时使用s
和joinResult
.
After this you can use both s
and joinResult
as you could with let
in query syntax.
这篇关于LINQ Let运算符具有lambda语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!