views:

82

answers:

2

Using EF4. I have a situation where a linq to EF query with explicit joins won't work due to a many-to-many table. I'm not going to break the database design to suit EF, so I want to use the include method. However, that always seems to generate left outer joins and I need inner joins (simple context.Table1s.Include("Table2") where tables are 1-to-1 or 1-to-many will demonstrate the problem).

Any way to force inner joins?

A: 

There is no way to make EF generate queries a certain way unfortunately.

However when you add the 3 entities you should only see 2 of them in the designer, the 3rd one is a "relation". So if you have for example the entities people, books and loans, you should only see people and books and people should have the navigation property called Books(), which returns all books related (loans) to that person.

And if you wish you could do an .Include("Books") on that navigation property aswell.

Fabian
A: 

You can control it by writing better LINQ

I've asked a very similar question that got me a usable answer that provided a solution so I was able to translate a query to an inner join hence speeding up query execution time considerably. Especially the second solution in the accepted answer is the one I used afterwards, because I didn't really want to use eSQL since I don't really like magic strings. It would put me back 10 years in the past.

Robert Koritnik
Sure if you write you query differently, but there is no property called .UseInnerJoinsOnly which you can set to true :P
Fabian
@Fabian: I guess writing LINQ efficiently is the same as writing other code efficiently. So in order to write good LINQ you have got to learn it deeper. The same goes for HTML, CSS, C#, T-SQL etc. If you know the technology you can write better code. LINQ is not just a one stop toolshop where they sell one-size-fits-all hammer and screwdriver. It's a regular toolshop that sells different tools for different purposes. And you have to know what you're doing to buy yourself the most appropriate tool.
Robert Koritnik
@Robert, amen to that!
Fabian
The answer to that other question uses linq to EF which I can't use in my situation. Nor (as you say) do I want to use eSQL.
dudeNumber4