How to orderby child property in EF
I basically want to write this in Lambda EF.
select c.* from company c
left join companyfeature cf
on c.companyID = cf.co开发者_C百科mpanyID
AND cf.FeatureID = 1
order by FeatureID desc, c.Name
I can't seem to figure it out.
In EF they are Companies & CompanyFeatures entitiesFor 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.
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 };
精彩评论