在单个查询中返回相关元素(我的查询不好吗?)

发布于 2024-09-11 11:42:44 字数 922 浏览 2 评论 0原文

嘿,我有一个名为 products 的表,它与其自身有关系。该表存储产品、其子产品以及子产品。当用户搜索最上面的产品时,此查询应该返回父产品、其子产品及其子产品的子产品。现在,我的查询可以工作,但我对实体框架和 SQL 不太熟悉,所以我不确定这是否应该这样做。为了让事情变得更加复杂,我正在表中的多个字段中进行搜索。

else
            {
                productQuery = 
                    from b in solutionContext.Version
                    where 
                        (
                            b.Product.Name == search || b.Product.Description == search ||
                            b.Product.Product2.Name == search || b.Product.Product2.Description == search ||
                            b.Product.Product2.Product2.Name == search || b.Product.Product2.Product2.Description == search
                        )
                    orderby b.Product.LastNumber ascending
                    select b;
            }

为了澄清起见,Product2 是从子级到父级的关系。

子问题:将来我想搜索一个孩子,返回其父母,以及父母的父母。我目前要做的方法是添加一些 lambda 表达式并执行我所做的操作,但要提升关系。这聪明吗?

Hey, I have a table called products that has a relationship with itself. This table stores products, their sub-products, and then sub-sub-products. When a user searches for the top-most product, this query is supposed to return the parent, its children, and its children's children. Now, my query works, but I am new to the entity framework and SQL in general, so I am not sure if this is the way it should be done. To make things a bit more hairy, I am searching against multiple fields in the table.

else
            {
                productQuery = 
                    from b in solutionContext.Version
                    where 
                        (
                            b.Product.Name == search || b.Product.Description == search ||
                            b.Product.Product2.Name == search || b.Product.Product2.Description == search ||
                            b.Product.Product2.Product2.Name == search || b.Product.Product2.Product2.Description == search
                        )
                    orderby b.Product.LastNumber ascending
                    select b;
            }

For clarification, Product2 is the relationship that goes from the child to the parent.

Subquestion: In the future I want to a search for a child, to return its parent, and parent's parent. The way I would currently go about doing that is to add some lambda expressions and do what I did, but going up the relationship. Is that smart?

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

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

发布评论

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

评论(1

随遇而安 2024-09-18 11:42:44

这个查询并没有什么问题。当然,L2E 中期望访问相关成员。

考虑使用如下语法:

b.Product.Name.Equals(search, StringComparison.OrdinalIgnoreCase)

搜索通常不区分大小写。

There's nothing really wrong with this query. Certainly, accessing related members is expected in L2E.

Consider using syntax like:

b.Product.Name.Equals(search, StringComparison.OrdinalIgnoreCase)

Searches should generally be case-insensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文