gpt4 book ai didi

c# - Lambda 表达式转换

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:50 31 4
gpt4 key购买 nike

我是 Linq 的初学者。

如何将此 lambda 表达式重写为 linq 编译查询?

var query5 = CustomerList.Select((cust, index) =>  new {cust, index})
.Where(c => c.cust.Country == "USA" && c.index > 70)
.Select(c => new { c.cust.CustomerID, c.cust.CompanyName,
c.index });

喜欢

var query5 = from c in .......
where .....
select c new {....}

最佳答案

这是最接近的查询表达式语法:

var query5 = from c in CustomerList.Select((cust, index) =>  new {cust, index})
where c.cust.Country == "USA" && c.index > 70
select new { c.cust.CustomerID, c.cust.CompanyName, c.index };

基本上,您不能在查询表达式语法中做的一点是“选择包括索引”重载。根本没有任何语法可以转化为那个。 (很多其他操作也是如此 - VB 的 LINQ 语法在这方面更丰富,尽管我个人对 C# 的处理方式很满意;它避免添加太多上下文关键字。)

(正如 Mehrdad 所说,这不是“已编译”查询。事实上,代码将被编译为完全相同的 IL。)

关于c# - Lambda 表达式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558655/

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