LINQ Let运算符具有lambda语法 [英] LINQ Let operator with lambda syntax

查看:159
本文介绍了LINQ Let运算符具有lambda语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道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一样在查询语法中同时使用sjoinResult.

After this you can use both s and joinResult as you could with let in query syntax.

这篇关于LINQ Let运算符具有lambda语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆