简体   繁体   English

LINQ to SQL in和not in

[英]LINQ to SQL in and not in

What is in and not in equals in LINQ to SQL ? 什么是innot in中等于LINQ到SQL

For example 例如

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

What is equal to the above statement in LINQ to SQL? 什么等于LINQ to SQL中的上述语句?

You use, where <list>.Contains( <item> ) 您使用, 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 '!' 对于'NOT'的情况,只需添加'!' operator before the 'Contains' statement. 'Contains'语句之前的运算符。

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. innot in在查询中的字段进行操作,但你不能在你的例子查询中指定的字段。 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: 在LINQ to SQL中等效的那些查询将是这样的:

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;

Please Try This For SQL Not IN 请尝试使用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)
   };

Please Try This For SQL IN 请尝试使用SQL IN

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM