Covert sql to entity framework

Esteban Peralta 26 Reputation points
2021-09-08T01:24:23.277+00:00

I have this working SQL query:

SELECT *
FROM Events
WHERE Events.Id NOT IN (SELECT Results.EventId
FROM Results
WHERE Results.UserId = 86);

I'm new to entity framework, so what would be it's equivalent?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,078 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,476 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,488 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-09-08T01:38:48.247+00:00

    You should be able to do something like this:

    db.Events
        .Where(e => !db.Results
            .Where(r => r.UserId =  86)
            .Select(r => r.EventId)
            .Contains(e.Id)
        );
    

    EF will turn that inner query to the equivalent sub-query in SQL.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful