tags:

views:

54

answers:

3

Does anyone know how to write this

var q = from c in customers
        join o in orders on c.Key equals o.Key
        select new {c.Name, o.OrderNumber};

In this syntax style?

var 1= customers.
    .join(???)
    .select(???)

I've been googling for a way to do this for days now with now luck. Everyone preferes the first syntax for tutorials, but I find the second much easier to determine order of operation when reading.

+3  A: 

The compiler translation process involves using a "transparent identifier" that makes the current customer and order available to the Select method. You can emulate this by making your own:

customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c, o })
         .Select(x => new { x.c.Name, x.o.OrderNumber });

Instead, you could just move your Name/OrderNumber projection up into the Join call:

customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c.Name, o.OrderNumber });
dahlbyk
Thanks for the multiple examples. It clarifies a bit more on how exactly it works.
Russell Steen
+2  A: 

This just takes a single call to Enumerable.Join:

var q = customers.Join(
                orders, 
                c => c.Key, 
                o => o.Key, 
                (c, o) => new {c.Name, o.OrderNumber}
             );
Reed Copsey
+2  A: 

You might also check out LinqPad . It has a small lambda button for the bottom half of the screen, and it translates the linq query to chain methods :

from p in PropertyListings
from rl in p.ResidentialListings 
select new {p.YearBuilt,p.ListingUrl}

got translated to :

PropertyListings
.SelectMany (
  p => p.ResidentialListings, 
  (p, rl) => 
     new  
     {
        YearBuilt = p.YearBuilt, 
        ListingUrl = p.ListingUrl
     }
)
sirrocco
+1 LinqPad rocks
Eric King