在 Linq to Entities 中将条件应用于预加载(包含)请求

发布于 2024-09-11 12:27:59 字数 643 浏览 8 评论 0原文

我有一个表产品。 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 技术交流群。

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

发布评论

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

评论(1

不羁少年 2024-09-18 12:27:59

使用投影,而不是预先加载。

var q = from p in Context.Product
        select new ProductPresentation
        {
           Id = p.Id,
           // etc.
           Description = new ProductDescriptionPresentation
           {
               Language = (from l in p.ProductDescription.Languages
                           where l.AlternateCode.Equals("es", StringComparison.OrdinalIgnoreCase)
                           select l).FirstOrDefault(),
               // etc.
           }
        };

Use a projection, not eager loading.

var q = from p in Context.Product
        select new ProductPresentation
        {
           Id = p.Id,
           // etc.
           Description = new ProductDescriptionPresentation
           {
               Language = (from l in p.ProductDescription.Languages
                           where l.AlternateCode.Equals("es", StringComparison.OrdinalIgnoreCase)
                           select l).FirstOrDefault(),
               // etc.
           }
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文