NOT IN sub select 从 T SQL 到 LINQ?
NOT IN sub select from T SQL to LINQ?
尝试在此处编写 LINQ 查询以从 T SQL 复制 NOT IN。这是我在 LINQ 中想要的 SQL 查询:
select * from schema.Table1
where TableId NOT IN (select pa.TableId from schema.Table2 ta
where ta.SecondaryId = myParameter)
看过以前的帖子,但还没完全弄明白。
var query = from a in _Context.Table1
where a.TableId != _Context.Table2.Any(ta=>(ta.SecondaryId== myParameter))
select a;
您可以尝试这样的操作:
// get all the values that you want to exclude in a list.
var excluded = (from t in _Context.TableId
select t.TableId
where t.SecondaryId == myParameter).ToList();
// get all the items from Table1 that aren't contained in the above list
var query = from t in _Context.Table1
where excluded.Contains(t.TableId)==false
select t;
或
// get all the values that you want to exclude in a list.
var excluded = _Context.TableId
.Where(x=>x.SecondaryId == myParameter)
.Select(x=>x.TableId)
.ToList();
// get all the items from Table1 that aren't contained in the above list
var query = _Context.Table1.Where(a=>!excluded.Any(a.TableId));
var query = from a in _Context.Table1
where _Context.Table2.Any(ta=>ta.SecondaryId== a.TableId) == false
select a;
您也许可以做到这一点...它并不漂亮...但考虑到 LinqToSQL 的有限功能集,它可能会起作用
IQueryable<Table2> excluded = from t in _Context.Table2
where t.SecondaryId == myParameter
select t;
// get all the items from Table1 that aren't contained in the above list
IQueryable<Table1> query = from t1 in _Context.Table1
join t2 in excluded on t1.TableId equals t2.TableId into foo
from e in foo.DefaultIfEmpty()
where e == null
select t;
此查询的结果应等同于
SELECT t1.*
FROM Table1 t1
LEFT JOIN (SELECT * FROM Table2 WHERE SecondaryId == @myParameter) t2
ON t1.TableId = t2.TableId
WHERE t2.TableId IS NULL
尝试在此处编写 LINQ 查询以从 T SQL 复制 NOT IN。这是我在 LINQ 中想要的 SQL 查询:
select * from schema.Table1
where TableId NOT IN (select pa.TableId from schema.Table2 ta
where ta.SecondaryId = myParameter)
看过以前的帖子,但还没完全弄明白。
var query = from a in _Context.Table1
where a.TableId != _Context.Table2.Any(ta=>(ta.SecondaryId== myParameter))
select a;
您可以尝试这样的操作:
// get all the values that you want to exclude in a list.
var excluded = (from t in _Context.TableId
select t.TableId
where t.SecondaryId == myParameter).ToList();
// get all the items from Table1 that aren't contained in the above list
var query = from t in _Context.Table1
where excluded.Contains(t.TableId)==false
select t;
或
// get all the values that you want to exclude in a list.
var excluded = _Context.TableId
.Where(x=>x.SecondaryId == myParameter)
.Select(x=>x.TableId)
.ToList();
// get all the items from Table1 that aren't contained in the above list
var query = _Context.Table1.Where(a=>!excluded.Any(a.TableId));
var query = from a in _Context.Table1
where _Context.Table2.Any(ta=>ta.SecondaryId== a.TableId) == false
select a;
您也许可以做到这一点...它并不漂亮...但考虑到 LinqToSQL 的有限功能集,它可能会起作用
IQueryable<Table2> excluded = from t in _Context.Table2
where t.SecondaryId == myParameter
select t;
// get all the items from Table1 that aren't contained in the above list
IQueryable<Table1> query = from t1 in _Context.Table1
join t2 in excluded on t1.TableId equals t2.TableId into foo
from e in foo.DefaultIfEmpty()
where e == null
select t;
此查询的结果应等同于
SELECT t1.*
FROM Table1 t1
LEFT JOIN (SELECT * FROM Table2 WHERE SecondaryId == @myParameter) t2
ON t1.TableId = t2.TableId
WHERE t2.TableId IS NULL