Home > ASP.Net, Linq > Use The NOT IN clause in LINQ to SQL

Use The NOT IN clause in LINQ to SQL


Hi all,

try this example to use the NOT IN clause in LINQ to SQL

This is one SQL query that returns that value.

SELECT * FROM [dbo].[Customers] AS c
WHERE c.[CustomerID] NOT IN (
SELECT o.[CustomerID]
FROM [dbo].[Orders] AS o
)

LINQ offers a Contains extension method that allows writing the following code.

NorthwindDataContext dbml = new NorthwindDataContext();
dc.Log = Console.Out;
var query =
from c in dbml.Customers
where !(from o in dbml.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;
foreach (var c in query) Console.WriteLine( c );

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Linq
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: