Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq equivalent sql query "not in (select query)"

Tags:

.net

sql

linq

my sql code is as follows:

select UserId,UserName 
from aspnet_Users 
where UserId not in (select UsersId from tbluser  where active='true')

what is the equivalent linq expression?

like image 260
decoder Avatar asked Dec 22 '12 09:12

decoder


People also ask

How does a LINQ query transform to a SQL query?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

Which is better SQL or LINQ?

The main difference between LINQ and SQL is that LINQ is a Microsoft . NET framework component, which adds native data querying capabilities to . NET languages, while SQL is a standard language to store and manage data in RDBMS.

Why we use LINQ instead of SQL?

Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++. Sure, there are times when it's still best to use C++ (as is the case with SQL), but in most situations, working in a modern tidy language and not having to worry about lower-level details is a big win.

Is LINQ faster than SQL?

Sql is faster than Linq. Its simple: if I m executing a sql query directly its a one way process whereas if I m using linq, first its been converted to sql query and then its executed.


1 Answers

my first try using LiNQ in C#

var result = from y in aspnet_Users
            where !(
                        from x in tblUser
                        where  x.active == "true"
                        select x.UsersID
                    ).Contains(y.UserId)
            select y;                
            -- OR // select new { y.UserId, y.UserName};

SOURCE

  • The NOT IN clause in LINQ to SQL
like image 56
John Woo Avatar answered Nov 14 '22 23:11

John Woo