在 Linq to Entities 中将条件应用于预加载(包含)请求
我有一个表产品。 Product 与 ProductDescription 存在一对多关系。每个产品的 ProductDescription 可以有多于一行。如果产品描述有多种翻译,它将有多行。产品与语言具有多对一的关系。语言具有语言代码(en、es 等)以及 LanguageId(也可在 ProductDescription 中找到)。
我想让我的用户能够请求产品,并进一步告诉应用程序仅返回特定语言的描述。
我在 Linq to Entities 中完成这个任务花了很长时间。我想生成这样的 SQL:
SELECT * FROM Product p
JOIN ProductDescription pd ON p.ProductId = pd.ProductId
JOIN (SELECT * FROM Language WHERE AlternateCode = 'es') AS l ON pd.LanguageId = l.LanguageId
(AlternateCode 是语言代码的字段名称)
有谁知道如何做到这一点?我现在剩下的就是拉下所有语言,然后使用 Linq to Objects 过滤掉它们,这肯定不太理想。我可以在循环中获取每个产品的语言代码,但这需要多次 SQL 往返,而我也不希望这样做。
感谢任何帮助!
I have a table Product. Product is related to ProductDescription in a one-to-many relationship. ProductDescription can have more than one row for each product. It will have multiple rows if there are multiple translations for the description of the product. Product has a many-to-one relationship with Language. Language has a language code (en, es, etc.) as well as a LanguageId (found in ProductDescription as well).
I want to give my users the ability to request a product and further tell the application to only return descriptions in a specific language.
I am having a bear of a time accomplishing this in Linq to Entities. I want to generate SQL like this:
SELECT * FROM Product p
JOIN ProductDescription pd ON p.ProductId = pd.ProductId
JOIN (SELECT * FROM Language WHERE AlternateCode = 'es') AS l ON pd.LanguageId = l.LanguageId
(AlternateCode is the field name for the language code)
Does anyone know how to do this? What I'm left with right now is pulling down all languages, then filtering them out with Linq to Objects which is less than ideal for sure. I could get the language codes per product in a loop but that would require multiple SQL round trips which I also don't want.
appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用投影,而不是预先加载。
Use a projection, not eager loading.