gpt4 book ai didi

linq - LINQ 连接中等号的左侧和右侧有什么区别

转载 作者:行者123 更新时间:2023-12-04 17:27:51 25 4
gpt4 key购买 nike

在 Linq 中进行连接时,例如
from c in customers join x in somelistofcustomers on x.Id equals c.Id
你会得到错误

x is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'



很简单,但我想澄清一下为什么 x 不在左侧的范围内,但不知何故在 equals 的右侧范围内

最佳答案

这与编译器将 LINQ 扩展为底层扩展方法的方式有关。

您的查询正在被翻译成:

customers.Join(somelistofcustomers, c => x.Id, x => c.Id, (c, x) => ...)

两个 lambda 表达式 c => x.Id & x => c.Id显然,它们的局部变量超出了范围。由于 LINQ 只是实际调用的一个很好的糖衣,编译器正确地提示变量超出范围。

关于linq - LINQ 连接中等号的左侧和右侧有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5214705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com