Linq'into'关键字混淆 [英] Linq 'into' keyword confusion

查看:93
本文介绍了Linq'into'关键字混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看同事的Linq查询,如下所示(查询正确执行):

I was looking at a co-workers Linq query, shown below (the query executes correctly):

from ea in EquipmentApplication
join erl in EquipmentRoutingLocation on ea.EquipmentID equals erl.EquipmentID into erlWithNulls

from erlAll in erlWithNulls.DefaultIfEmpty()
join rl in RoutingLocation on erlAll.RoutingLocationID equals rl.RoutingLocationID into rlWithNulls

from rlAll in rlWithNulls.DefaultIfEmpty()
where ea.Equipment.Master_Cell.Area.Unit.UnitID == 1160
select new { ea.Equipment, ea.ApplicationFriendlyName, rlAll }

我对为什么这样做感到困惑.我的理解(可能是错误的)是,"into"关键字结束了当前的作用域/上下文(并且创建的任何变量现在都超出了作用域)并创建了一个新的变量.如果是这样,为什么在查询的最后一部分中'ea'变量仍在作用域内?

I'm confused as to why this works. My understanding (perhaps incorrectly) is that the 'into' keyword ends the current scope/context (and any variables created are now out of scope) and creates a new one. If this is true, why is the 'ea' variable still in scope in the last part of the query?

推荐答案

select关键字一起使用时,into将终止作用域.
join关键字一起使用时,into将添加一个变量,该变量包含联接中的所有匹配项. (这称为组加入)

When used with the select keyword, into will end the scope.
When used with the join keyword, into will add a variable containing all of the matching items from the join. (This is called a Group Join)

这篇关于Linq'into'关键字混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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