Linq join operator type with lambda syntax

I code my way in the MS 101 Linq tutorial.

I am trying to reorganize a request for lambda / method syntax (and vice versa). This one is a challenge for me.

This request:

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 };

What I rewrote like this:

var custSupQuery = suppliers.Join(customers, s => s.Country, c => c.Country, (c, s) => new { Key = s.Country, Items = s, Items2 = c });

(I did not see an easy way to combine the fields in both sentences in both cases, so I left them separately).

It seems that it flies with the compiler until it hits the display loops. The second foreach file cannot handle the type.

Here is the mapping code (which works with the query expression, but not with the lambda / method syntax):

foreach (var item in custSupQuery)
{
    Console.WriteLine(item.Key + ":");
    foreach (var element in item.Items)  // <-- error here
    {
        Console.WriteLine("   " + element.CompanyName);
    }
}

Error:

foreach "JoinOperators.Program.Customer", "JoinOperators.Program.Customer" 'GetEnumerator'

/ AsEnumerable(), - . , AsEnumerator<type_goes_here>, , , GetType() on.

?

+5
1

join ... into Join, GroupJoin. , :

var custSupQuery =
    suppliers.GroupJoin(customers, s => s.Country, c => c.Country,
                        (s, cs) => new { Key = s.Country, Items = cs });
+5

All Articles