Using pubs
If I want to join using query syntax I would do this.
from a in db.authors
join ta in db.titleauthors on a.au_id equals ta.au_id
join t in db.titles on ta.title_id equals t.title_id
join s in db.sales on t.title_id = s.title_id
select new { a.au_lname, t.title1, s.qty }
Using method syntax
db.authors
.Join(db.titleauthors,
a => a.au_id,
ta => ta.au_id,
(a, ta) => new {a, ta})
.Join(db.titles,
z => z.ta.title_id,
t => t.title_id,
(z, t) => new { z.a, z.ta, t })
.Join(db.sales,
z => z.t.title_id,
s => s.title_id,
(z, s) => new { z.a, z.ta, z.t, s })
.Select(z => new { z.a.au_lname, z.t.title1, z.s.qty })
I was wondering if there was an elegant way of dealing with this line
(z, X) => new { z.Y1, z.Y2, z.Y3... , X }
maybe something like
(z, X) => z.push(X)
so I don’t have to write everything out.
Does something like that exist or is possible?
No. Basically query expressions exist in order to keep this stuff away from you. There’s no particularly simple way of mimicking transparent identifiers.
I find that when you hit transparent identifiers, it’s almost always cleaner to use query expression syntax. It’s definitely worth knowing both, as very simple queries are cleaner using method syntax, but the more complicated the query, the more likely it is to be easier to read using query expressions.
That’s assuming it can all be represented with method expressions, of course. Don’t forget you can break up queries into separate statements without changing the meaning, so if you do need to call methods which don’t have query expression equivalents, I sometimes find it best to separate it out like this:
I find that cleaner than just using brackets to mush the two syntax forms together.