如何在 EF 中按子属性排序
我基本上想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个(请注意,这是 C#,但 VB 非常相似。):
Try this (Note that this is C# but VB is quite similar.):