实体框架分页联接和联合?
我正在为具有以下结构的数据库开发一个新的 Web 界面:
Categories Table (ID, Name, Active, ...)
Products Table (ID, Name, ItemNo, Price, Active, ...)
WebPages Table (ID, Name, Active, ...)
Links Table (ID, Name, Href, Target, Active, ...)
Relations Table (ID, ParentCategoryID, CmsForeignKeyID, CmsItemType, SortOrder, Visible)
关系表用于建立父类别与其子类别之间的关系,这些子类别可以是类别、产品、网页或链接。
我需要能够获取给定父类别的子节点的分页列表。节点必须是活动的、可见的且已排序(默认排序顺序,然后按名称排序)。
我尝试使用 EF/LINQ 做一些事情,但由于需要多个条件(例如:R.CmsForeignKeyID = N.ID,R.CmsItemType = 1),所以在 JOINing 关系方面遇到了问题,所以我什至没有进行联合和分页。
在这种情况下,最好放弃 EF 作为工具,而使用传统 SQL 来实现,例如:
SELECT C.CategoryID As ID, R.CmsItemTypeID, C.Name, R.SortOrder, '' AS Href, '' As LinkTarget, null As MSRP, null As ListPrice, null As Price
FROM kc_Relations R
JOIN kc_Categories C ON R.CmsItemID = C.CategoryID AND R.CmsItemTypeID = 1
WHERE C.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT W.WebPageID As ID, R.CmsItemTypeID, W.Name, R.SortOrder, '' AS Href, '' As LinkTarget, null AS MSRP, null As ListPrice, null As Price
FROM kc_Relations R
JOIN kc_WebPages W ON R.CmsItemID = W.WebPageID AND R.CmsItemTypeID = 0
WHERE W.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT P.ProductID As ID, R.CmsItemTypeID, P.Name, R.SortOrder, '' AS Href, '' As LinkTarget, P.MSRP, P.ListPrice, P.Price
FROM kc_Relations R
JOIN kc_Products P ON R.CmsItemID = P.ProductID AND R.CmsItemTypeID = 2
WHERE P.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT L.LinkID As ID, R.CmsItemTypeID, L.Name, R.SortOrder, L.Href, L.Target As LinkTarget, null As MSRP, null AS ListPrice, null AS Price
FROM kc_Relations R
JOIN kc_Links L ON R.CmsItemID = L.LinkID AND R.CmsItemTypeID = 3
WHERE L.IsActive = 1 AND R.IsVisible = 1
然后对结果集执行分页逻辑?或者是否有更好的方法来构建数据库以使这变得更容易?
I'm developing a new web interface for a database with the following structure:
Categories Table (ID, Name, Active, ...)
Products Table (ID, Name, ItemNo, Price, Active, ...)
WebPages Table (ID, Name, Active, ...)
Links Table (ID, Name, Href, Target, Active, ...)
Relations Table (ID, ParentCategoryID, CmsForeignKeyID, CmsItemType, SortOrder, Visible)
The Relations table is used to establish relations between a parent category and its children, which can be categories, products, webpages, or links.
I need to be able to get back a paged list of the child nodes for a given parent category. The nodes have to be Active, Visible, and Sorted (default SortOrder then by Name).
I tried doing some things with EF/LINQ and had issues JOINing Relations because of needing multiple criteria (example: R.CmsForeignKeyID = N.ID, R.CmsItemType = 1) so I didn't even get to the unions and paging.
Would it be best to forego EF as a tool in this scenario and do it with traditional SQL, something like:
SELECT C.CategoryID As ID, R.CmsItemTypeID, C.Name, R.SortOrder, '' AS Href, '' As LinkTarget, null As MSRP, null As ListPrice, null As Price
FROM kc_Relations R
JOIN kc_Categories C ON R.CmsItemID = C.CategoryID AND R.CmsItemTypeID = 1
WHERE C.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT W.WebPageID As ID, R.CmsItemTypeID, W.Name, R.SortOrder, '' AS Href, '' As LinkTarget, null AS MSRP, null As ListPrice, null As Price
FROM kc_Relations R
JOIN kc_WebPages W ON R.CmsItemID = W.WebPageID AND R.CmsItemTypeID = 0
WHERE W.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT P.ProductID As ID, R.CmsItemTypeID, P.Name, R.SortOrder, '' AS Href, '' As LinkTarget, P.MSRP, P.ListPrice, P.Price
FROM kc_Relations R
JOIN kc_Products P ON R.CmsItemID = P.ProductID AND R.CmsItemTypeID = 2
WHERE P.IsActive = 1 AND R.IsVisible = 1
UNION
SELECT L.LinkID As ID, R.CmsItemTypeID, L.Name, R.SortOrder, L.Href, L.Target As LinkTarget, null As MSRP, null AS ListPrice, null AS Price
FROM kc_Relations R
JOIN kc_Links L ON R.CmsItemID = L.LinkID AND R.CmsItemTypeID = 3
WHERE L.IsActive = 1 AND R.IsVisible = 1
And then do the paging logic on the resultset? Or is there a better way to structure the DB that will make this easier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对观点的思考为我指明了正确的方向。经过一些额外的测试,我能够让它工作。您必须在 LINQ 查询的 SELECT 部分中为 UNION 的每个部分设置相同的属性才能使其正常工作。如果没有值,您仍然需要手动将其设置为默认值(空)。例子:
My thought on views kind of pointed me in the right direction. After some additional testing, I was able to get this to work. You have to set the same properties in the SELECT portion of the LINQ query for each part of the UNION in order for it to work. If you don't have a value, you still have to manually set it to a default (null). Example: