代码之家  ›  专栏  ›  技术社区  ›  Pranay Rana

LINQ到SQL in和not in

  •  9
  • Pranay Rana  · 技术社区  · 15 年前

    是什么 in not in 等于 LINQ to SQL

    例如

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

    在linqtosql中,什么与上面的语句相等?

    3 回复  |  直到 9 年前
        1
  •  25
  •   KJSR    7 年前

    你用, where <list>.Contains( <item> )

    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”语句之前的运算符。

        2
  •  8
  •   Randolpho    15 年前

    in not in 对查询中的字段进行操作,但在示例查询中没有指定字段。所以应该是这样的:

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

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

    那些 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;
    

    List<int> validValues = new List<int>() { 1, 2};
    var qry = from item in dataContext.TableName
              where !validValues.Contains(item.FieldName)
              select item;
    
        3
  •  0
  •   Md. Nazrul Islam    12 年前

    请给我试试这个 SQL不在

    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)
       };
    

    请给我试试这个

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