在单个查询中返回相关元素(我的查询不好吗?)
嘿,我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个查询并没有什么问题。当然,L2E 中期望访问相关成员。
考虑使用如下语法:
搜索通常不区分大小写。
There's nothing really wrong with this query. Certainly, accessing related members is expected in L2E.
Consider using syntax like:
Searches should generally be case-insensitive.