C# 具有lambda语法的linq联接运算符类型
我正在编写MS101 Linq教程 我尝试将查询重构为lambda/方法语法(反之亦然)。一个是对我的挑战 给定的查询是:C# 具有lambda语法的linq联接运算符类型,c#,linq,C#,Linq,我正在编写MS101 Linq教程 我尝试将查询重构为lambda/方法语法(反之亦然)。一个是对我的挑战 给定的查询是: var custSupQuery = from sup in suppliers join cust in customers on sup.Country equals cust.Country into cs select new { Key = sup.Country, Items = cs }; 我是这样写的: var custSupQue
var custSupQuery =
from sup in suppliers
join cust in customers on sup.Country equals cust.Country into cs
select new { Key = sup.Country, Items = cs };
我是这样写的:
var custSupQuery = suppliers.Join(customers, s => s.Country, c => c.Country, (c, s) => new { Key = s.Country, Items = s, Items2 = c });
(在新子句中,我没有找到一种简单的方法来组合这两种类型的字段,所以我将它们分开)
这似乎与编译器一起运行,直到它到达显示循环。第二个foreach似乎无法处理该类型
以下是显示代码(可用于查询表达式,但不适用于lambda/method语法):
foreach(custSupQuery中的var项)
{
Console.WriteLine(item.Key+“:”);
foreach(item.Items中的var元素)/语法中的join…to
并不等同于join
而是to。这就是您必须使用的:
var custSupQuery =
suppliers.GroupJoin(customers, s => s.Country, c => c.Country,
(s, cs) => new { Key = s.Country, Items = cs });
s只是一个东西,不是一堆东西。谢谢!当我在博客上写这篇文章的时候,我会给你一个大喊
var custSupQuery =
suppliers.GroupJoin(customers, s => s.Country, c => c.Country,
(s, cs) => new { Key = s.Country, Items = cs });