在 LINQ 查询中使用 'NOT IN' 子句
Using 'NOT IN' Clause in LINQ Query
我已经在 LINQPad 中测试了以下 LINQ 查询。但它给了我这个错误:
Error Compiling Expression: Error Compiling Expression: The best overloaded method match for 'string.Contains(string)' has some invalid arguments
Argument '1': cannot convert from 'System.Linq.IQueryable' to 'string'
(from t in db.ASN_ITEM
join t0 in db.ASN_MASTER on t.AWB_NO equals t0.AWB_NO
join t1 in db.ITEM_MASTER on t.ITEM_MASTER.ITEM_CODE equals t1.ITEM_CODE
where
(t.TOT_QTY - t.SCND_QTY) != 0 &&
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct()) &&
t.AWB_NO == "E1000001"
select new {
t.AWB_NO,
ASN_DATE = t.REC_DATE,
t.PALLET,
t.CARTON,
t.TOT_QTY,
t.SCND_QTY,
VAR_QTY = (System.Int32?)(t.SCND_QTY - t.TOT_QTY),
REMARKS =
(t.TOT_QTY - t.SCND_QTY) == 0 ? "No Variance" :
(t.TOT_QTY - t.SCND_QTY) > 0 ? "Less Qty" :
(t.TOT_QTY - t.SCND_QTY) < 0 &&
t.TOT_QTY != 0 ? "Excess Qty" :
t.TOT_QTY == 0 &&
t.SCND_QTY != 0 ? "Excess Item" : null,
t1.PART_NO
}).Distinct()
当我在 where 子句中给出以下条件时,我得到了这个错误:
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct())
实际上在SQL查询中,这就是我需要的(下):
WHERE ASN_ITEM.AWB_NO NOT IN (SELECT DISTINCT AWB_NO FROM ASN_ITEM WHERE SCAN_STAT !=2 )
你应该反过来做。在子查询上调用 Contains()
,其中有多个项目:
!(from t2 in db.ASN_ITEM
where t2.SCAN_STAT != 2
select t2.AWB_NO
).Distinct()
.Contains(t.AWB_NO)
此外,您必须直接 select AWB_NO
,如上所示,而不是投影到匿名类型。执行后者将阻止 Contains()
的使用,因为集合中的项目类型将不同于作为 Contains()
.
参数传递的对象类型
从您的代码中,我了解到 AWB_NO
是一个字符串。如果是这样,那么你在这里做什么:
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct())
翻译为:"It's not true that a string ABW_NO
contains (and it's this Contains
, not this) a table of some distinct elements of anonymous type with one string property"。这是没有意义的。这不是 "not in" 查询。您正在检查单个字符串是否包含一组匿名类型对象。
如果你想使用 String.Contains
并检查一个字符串是否包含另一个字符串,那么这将更有意义(虽然可能不是你想要的):
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select t2.AWB_NO).FirstOrDefault())
您可能会发现这有帮助:
How would you do a "not in" query with LINQ?
"NOT IN" clause in LINQ to Entities
我已经在 LINQPad 中测试了以下 LINQ 查询。但它给了我这个错误:
Error Compiling Expression: Error Compiling Expression: The best overloaded method match for 'string.Contains(string)' has some invalid arguments Argument '1': cannot convert from 'System.Linq.IQueryable' to 'string'
(from t in db.ASN_ITEM
join t0 in db.ASN_MASTER on t.AWB_NO equals t0.AWB_NO
join t1 in db.ITEM_MASTER on t.ITEM_MASTER.ITEM_CODE equals t1.ITEM_CODE
where
(t.TOT_QTY - t.SCND_QTY) != 0 &&
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct()) &&
t.AWB_NO == "E1000001"
select new {
t.AWB_NO,
ASN_DATE = t.REC_DATE,
t.PALLET,
t.CARTON,
t.TOT_QTY,
t.SCND_QTY,
VAR_QTY = (System.Int32?)(t.SCND_QTY - t.TOT_QTY),
REMARKS =
(t.TOT_QTY - t.SCND_QTY) == 0 ? "No Variance" :
(t.TOT_QTY - t.SCND_QTY) > 0 ? "Less Qty" :
(t.TOT_QTY - t.SCND_QTY) < 0 &&
t.TOT_QTY != 0 ? "Excess Qty" :
t.TOT_QTY == 0 &&
t.SCND_QTY != 0 ? "Excess Item" : null,
t1.PART_NO
}).Distinct()
当我在 where 子句中给出以下条件时,我得到了这个错误:
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct())
实际上在SQL查询中,这就是我需要的(下):
WHERE ASN_ITEM.AWB_NO NOT IN (SELECT DISTINCT AWB_NO FROM ASN_ITEM WHERE SCAN_STAT !=2 )
你应该反过来做。在子查询上调用 Contains()
,其中有多个项目:
!(from t2 in db.ASN_ITEM
where t2.SCAN_STAT != 2
select t2.AWB_NO
).Distinct()
.Contains(t.AWB_NO)
此外,您必须直接 select AWB_NO
,如上所示,而不是投影到匿名类型。执行后者将阻止 Contains()
的使用,因为集合中的项目类型将不同于作为 Contains()
.
从您的代码中,我了解到 AWB_NO
是一个字符串。如果是这样,那么你在这里做什么:
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select new {
t2.AWB_NO
}).Distinct())
翻译为:"It's not true that a string ABW_NO
contains (and it's this Contains
, not this) a table of some distinct elements of anonymous type with one string property"。这是没有意义的。这不是 "not in" 查询。您正在检查单个字符串是否包含一组匿名类型对象。
如果你想使用 String.Contains
并检查一个字符串是否包含另一个字符串,那么这将更有意义(虽然可能不是你想要的):
!t.AWB_NO.Contains((from t2 in db.ASN_ITEM
where
t2.SCAN_STAT != 2
select t2.AWB_NO).FirstOrDefault())
您可能会发现这有帮助:
How would you do a "not in" query with LINQ?
"NOT IN" clause in LINQ to Entities