NibblyPig
NibblyPig

Reputation: 52922

Could I have some help converting inner joins in sql into linq?

I want to use chained linq. I am having some trouble with the syntax.

select * from APPLES
inner join BANANAS on APPLES.id = BANANAS.someid

I have:

var result = workspace.GetDataSource<APPLE>().Join(.......)

but I am unsure about what goes into the Join bit. Could someone help me out?

Upvotes: 0

Views: 133

Answers (1)

Jeff Machamer
Jeff Machamer

Reputation: 942

How about:

var result = from a in workspace.GetDataSource<APPLE>()
             from b in workspace.GetDataSource<BANANAS>()
             where a.id == b.someid
             select a;

or if you want to use join notation:

var result = from a in workspace.GetDataSource<APPLE>()
             join b in workspace.GetDataSource<BANANAS>()
             on a.id equals b.someid
             select a;

Note, you can change the select a to a projection of the elements you need from both tables.

Not as familiar with this format, but it would be something like this:

var result = workspace.GetDataSource<APPLE>().Join(workspace.GetDataSource<BANANAS>(),a=>a.id, b=>b.someid,(a,b) => a);

Upvotes: 4

Related Questions