LINQ to SQL in and not in
Asked Answered
J

3

13

What is in and not in equals in LINQ to SQL?

For example

select * from table in ( ...)
and 
select * from table not in (..)

What is equal to the above statement in LINQ to SQL?

Jural answered 15/6, 2010 at 17:51 Comment(0)
C
28

You use, where <list>.Contains( <item> )

var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                 select p;

Or you can have a list predefined as such:

int[] ids = {1, 2, 3};

var query = from item in context.items
            where ids.Contains( item.id )
            select item;

For the 'NOT' case, just add the '!' operator before the 'Contains' statement.

Chaille answered 15/6, 2010 at 17:55 Comment(5)
How do you handle if it's in a list? IN('a', 'b', 'c')Jacobin
@Jacobin he just mentioned how you handle if it's in a list.Fanni
this only equal to in clause what is for not inJural
See my updated statement for the 'NOT' condition. Essentially, you just need to add '!' before the Contains call.Chaille
For .NET Version 3.5 have a look at blogs.msdn.microsoft.com/alexj/2009/03/25/…Byrnie
F
8

I'm confused by your question. in and not in operate on fields in the query, yet you're not specifying a field in your example query. So it should be something like:

select * from table where fieldname in ('val1', 'val2')

or

select * from table where fieldname not in (1, 2)

The equivalent of those queries in LINQ to SQL would be something like this:

List<string> validValues = new List<string>() { "val1", "val2"};
var qry = from item in dataContext.TableName
          where validValues.Contains(item.FieldName)
          select item;

and this:

List<int> validValues = new List<int>() { 1, 2};
var qry = from item in dataContext.TableName
          where !validValues.Contains(item.FieldName)
          select item;
Fanni answered 15/6, 2010 at 17:55 Comment(0)
H
0

Please Try This For SQL Not IN

var v = from cs in context.Sal_Customer
         join sag in context.Sal_SalesAgreement
         on cs.CustomerCode equals sag.CustomerCode
         where
          !(
               from cus in context.Sal_Customer
               join
               cfc in context.Sal_CollectionFromCustomers
               on cus.CustomerCode equals cfc.CustomerCode
               where cus.UnitCode == locationCode &&
                     cus.Status == Helper.Active &&
                     cfc.CollectionType == Helper.CollectionTypeDRF
               select cus.CustomerCode
           ).Contains(cs.CustomerCode) &&
           cs.UnitCode == locationCode &&
           cs.Status == customerStatus &&
           SqlFunctions.DateDiff("Month", sag.AgreementDate, drfaDate) < 36
           select new CustomerDisasterRecoveryDetails
           {
             CustomerCode = cs.CustomerCode,
             CustomerName = cs.CustomerName,
             AgreementDate = sag.AgreementDate,
             AgreementDuration = SqlFunctions.DateDiff("Month", sag.AgreementDate, drfaDate)
   };

Please Try This For SQL IN

context.Sal_PackageOrItemCapacity.Where(c => c.ProjectCode == projectCode && c.Status == Helper.Active && c.CapacityFor.Contains(isForItemOrPackage)).ToList();
Hemispheroid answered 19/6, 2013 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.