Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有lambda语法的linq联接运算符类型_C#_Linq - Fatal编程技术网

C# 具有lambda语法的linq联接运算符类型

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

我正在编写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 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 });