2010-06-15 12 views

risposta

21

Si utilizza, where <list>.Contains(<item>)

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

o si può avere un elenco predefinito in quanto tale: '!'

var ids = {1, 2, 3}; 

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

Per la 'NON E' caso, è sufficiente aggiungere il operatore prima dell'istruzione "Contiene".

+0

Come si fa a gestire se è in una lista? IN ('a', 'b', 'c') – DOK

+0

@DOK ha appena menzionato come gestisci se è in una lista. – Randolpho

+0

Vedere il mio esempio aggiornato. –

8

Sono confuso dalla tua domanda. in e not in funzionano sui campi nella query, ma non si specifica un campo nella query di esempio. Così dovrebbe essere qualcosa di simile:

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

o

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

L'equivalente di quei query in LINQ to SQL potrebbe essere qualcosa di simile:

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

e questo:

List<int> validValues = new List<int>() { 1, 2}; 
var qry = from item in dataContext.TableName 
      where !validValues.Contains(item.FieldName) 
      select item; 
-1

prega Prova questo per SQL non 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) 
    }; 

prega Prova questo per SQL IN

context.Sal_PackageOrItemCapacity.Where(c => c.ProjectCode == projectCode && c.Status == Helper.Active && c.CapacityFor.Contains(isForItemOrPackage)).ToList();