[c#]NOT IN 子选择从 T-SQL LINQ 到吗?

标签: Linq SQLServer C#
发布时间: 2017/2/28 20:14:44
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

试图写在这里的 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;

解决方法 1:

你可以尝试这样的事情︰

// 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));
赞助商