LINQ子查询问题
谁能告诉我如何获取第一个语句中第二个语句中没有的记录(见下文)?
from or in TblOrganisations
where or.OrgType == 2
select or.PkOrgID
第二次查询:
from o in TblOrganisations
join m in LuMetricSites
on o.PkOrgID equals m.FkSiteID
orderby m.SiteOrder
select o.PkOrgID
Can anybody tell me how I would get the records in the first statement that are not in the second statement (see below)?
from or in TblOrganisations
where or.OrgType == 2
select or.PkOrgID
Second query:
from o in TblOrganisations
join m in LuMetricSites
on o.PkOrgID equals m.FkSiteID
orderby m.SiteOrder
select o.PkOrgID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要 ID,那么
Except
应该可以解决问题:请注意,
Except
将两个序列视为集合。这意味着first
中的任何重复元素都不会包含在结果中。我怀疑这不会成为问题,因为名称PkOrgID
暗示了某种唯一的 ID。(请参阅
Enumerable.Except
和
Queryable.Except
了解更多信息。)If you only need the IDs then
Except
should do the trick:Note that
Except
treats the two sequences as sets. This means that any duplicate elements infirst
won't be included in the results. I suspect that this won't be a problem since the namePkOrgID
suggests a unique ID of some kind.(See the documentation for
Enumerable.Except
andQueryable.Except
for more info.)您需要完整的记录,还是只需要 ID? ID 很简单...
编辑:好的,如果你做不到这一点,你将需要类似的东西:
检查它生成的 SQL,但我认为它应该做正确的事情。请注意,在第二个查询中执行任何排序都是没有意义的,甚至是针对 TblOrganizations 的联接也是没有意义的。换句话说,您可以使用:
Do you need the whole records, or just the IDs? The IDs are easy...
EDIT: Okay, if you can't do that, you'll need something like:
Check the SQL it produces, but I think it should do the right thing. Note that there's no point in performing any ordering in the second query - or even the join against TblOrganisations. In other words, you could use:
使用
例外
:Use
Except
: