LINQ to SQL in in和not in

LINQ to SQL中有什么和not in等于?

例如

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

什么等于LINQ to SQL中的上述语句?

你使用, where .Contains( )

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

或者您可以预定义列表:

 int[] ids = {1, 2, 3}; var query = from item in context.items where ids.Contains( item.id ) select item; 

对于’NOT’的情况,只需添加’!’ ‘Contains’语句之前的运算符。

我对你的问题感到困惑。 not in操作查询中的字段,但您没有在示例查询中指定字段。 所以它应该是这样的:

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

要么

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

在LINQ to SQL中等效的那些查询将是这样的:

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

还有这个:

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

请尝试使用SQL而不是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) }; 

请尝试使用SQL IN

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