Skip to content
Advertisement

How to convert SQL query to Linq in Entity Framework 6?

How can I convert this SQL query to a linq expression?

Yes, I know about the Database.SqlQuery<>() function. Probably it’s the best solution in terms of performance. But code uniformity is preferred, so I need to use linq.

Here is my solution:

But maybe it`s possible to do it more elegantly?

Advertisement

Answer

This is all freehand so I apologize if there are some syntax issues…

Assuming you are using EF/EFCore and your classes are structured similar to the following:

Then something like the following should match your SQL query?:

You could also distinct the result, but assuming that ‘Id’ is your primary key for Client then it shouldn’t be necessary as they will be unique.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement