LINQ子查询问题

发布于 2024-10-18 13:20:04 字数 297 浏览 1 评论 0原文

谁能告诉我如何获取第一个语句中第二个语句中没有的记录(见下文)?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2024-10-25 13:20:04

如果您只需要 ID,那么 Except 应该可以解决问题:

var inFirstButNotInSecond = first.Except(second);

请注意,Except 将两个序列视为集合。这意味着 first 中的任何重复元素都不会包含在结果中。我怀疑这不会成为问题,因为名称 PkOrgID 暗示了某种唯一的 ID。

(请参阅 Enumerable.ExceptQueryable.Except 了解更多信息。)

If you only need the IDs then Except should do the trick:

var inFirstButNotInSecond = first.Except(second);

Note that Except treats the two sequences as sets. This means that any duplicate elements in first won't be included in the results. I suspect that this won't be a problem since the name PkOrgID suggests a unique ID of some kind.

(See the documentation for Enumerable.Except and Queryable.Except for more info.)

ぶ宁プ宁ぶ 2024-10-25 13:20:04

您需要完整的记录,还是只需要 ID? ID 很简单...

var ids = firstQuery.Except(secondQuery);

编辑:好的,如果你做不到这一点,你将需要类似的东西:

var secondQuery = ...; // As you've already got it

var query = from or in TblOrganisations
            where or.OrgType == 2
            where !secondQuery.Contains(or.PkOrgID)
            select ...;

检查它生成的 SQL,但我认为它应该做正确的事情。请注意,在第二个查询中执行任何排序都是没有意义的,甚至是针对 TblOrganizations 的联接也是没有意义的。换句话说,您可以使用:

var query = from or in TblOrganisations
            where or.OrgType == 2
            where !LuMetricSites.Select(m => m.FkSiteID).Contains(or.PkOrgID)
            select ...;

Do you need the whole records, or just the IDs? The IDs are easy...

var ids = firstQuery.Except(secondQuery);

EDIT: Okay, if you can't do that, you'll need something like:

var secondQuery = ...; // As you've already got it

var query = from or in TblOrganisations
            where or.OrgType == 2
            where !secondQuery.Contains(or.PkOrgID)
            select ...;

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:

var query = from or in TblOrganisations
            where or.OrgType == 2
            where !LuMetricSites.Select(m => m.FkSiteID).Contains(or.PkOrgID)
            select ...;
落日海湾 2024-10-25 13:20:04

使用例外

var filtered = first.Except(second);

Use Except:

var filtered = first.Except(second);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文