在 LINQ to SQL 中,in
和 not in
的含义是什么?
例如:
select * from table in ( ...)
and
select * from table not in (..)
如何在 LINQ to SQL 中表示与上述语句等价的查询?
在 LINQ to SQL 中,in
和 not in
的含义是什么?
例如:
select * from table in ( ...)
and
select * from table not in (..)
如何在 LINQ to SQL 中表示与上述语句等价的查询?
你使用 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”语句之前添加“!”运算符。
in
和not in
是针对字段进行操作的,但在你的示例查询中没有指定字段。因此应该像这样写:
我对你的问题感到困惑。在查询中,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;
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();