如何在 EF 中按子属性排序

发布于 2024-10-31 01:45:01 字数 621 浏览 0 评论 0原文

我基本上想用 Lambda EF 来写这个。

select c.* from company c
left join companyfeature cf 
on c.companyID = cf.companyID
AND cf.FeatureID = 1
order by FeatureID desc, c.Name

我似乎无法弄清楚。
在 EF 中,它们是 Companies & CompanyFeatures 实体

对于 Claus: 我从来没有说过我没有尝试过,我只是说我无法弄清楚。但为了证明我不是你的免费装载者;这是我的 linq 语句。 (是的,我已经准备好了 FK)

Companies
.OrderByDescending(c => c.CompanyFeatures.Any(f => f.FeatureID == 1))
.ThenBy(c => c.Name)

这确实有效,但它会产生一个不合时宜的 SQL 语句,它需要 6 秒,而上面的 SQL 语句则需要 1 毫秒。所以我认为我写错了。我知道有很多比我聪明的人,所以我希望有人愿意分享他们的知识。提前致谢。

I basically want to write this in Lambda EF.

select c.* from company c
left join companyfeature cf 
on c.companyID = cf.companyID
AND cf.FeatureID = 1
order by FeatureID desc, c.Name

I can't seem to figure it out.
In EF they are Companies & CompanyFeatures entities

For Claus:
I never said I didn't try, I said I couldn't figure it out. But to prove I'm not some free loader to you; here is my linq statement. (Yes I have my FK in place)

Companies
.OrderByDescending(c => c.CompanyFeatures.Any(f => f.FeatureID == 1))
.ThenBy(c => c.Name)

That actually works, but it produces an ungodly SQL statement that takes 6 seconds vs ms for my SQL statement above. So I assume I've wrote it incorrectly. I know there are a lot smarter people out there than me so I'm hoping someone will be willing to share their knowledge. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

深空失忆 2024-11-07 01:45:01

试试这个(请注意,这是 C#,但 VB 非常相似。):

 var result = 
    from Company in Companies
    from CompanyFeature in Company.CompanyFeatures.Where(cf => cf.FeatureID == 1).DefaultIfEmpty()
    orderby Company.Name
    select { Company, CompanyFeature };

Try this (Note that this is C# but VB is quite similar.):

 var result = 
    from Company in Companies
    from CompanyFeature in Company.CompanyFeatures.Where(cf => cf.FeatureID == 1).DefaultIfEmpty()
    orderby Company.Name
    select { Company, CompanyFeature };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文