1

我正在使用 Microsoft PUBS 数据库,其中有两个关系表发布者和 pub_info 发布者在 pub_id 字段上有主键,而 pub_info 有发布者表 pub_id 的外键

我正在尝试使用 linq 查询获取发布者表中可用但 pub_info 表中不可用的记录我知道 sql 中的查询

select pub_id from publisher where pub_id not in(select pub_id from pub_info);

var data =db.publisher.where(x=>db.pub_info.select(x1=>x1.pub_id).Contains(x)).Select(x.pub_id)

执行此查询时出错

但我无法了解在 linq 中生成相同的查询

4

2 回答 2

1

您也许可以这样做:

var data=(from p in db.publisher
          where !(from pi in db.pub_info
                  select pi.pub_id
                  ).Contains(p.pub_id)
          select p.pub_id
         );

或者这也可能有效:

var data=(from p in db.publisher
          where !db.pub_info.Select(pi => pi.pub_id).Contains(p.pub_id)
          select p.pub_id
         );
于 2012-04-05T13:18:03.570 回答
1

我认为你可以做这样的事情

var excludedIds = db.pub_info.Select(pi => pi.pub_id).ToArray();

var data = db.publisher.Where(p => !excludedIds.Contains(p.pub_id);

我可能是错的,但我想我记得它不能在一轮中完成。

编辑:不是一轮意味着我认为你必须“枚举”excludedIds。但是你当然可以在一个查询中写出来。

于 2012-04-05T13:11:48.307 回答