获取所有祖父ID等于1的子代。

4

我在数据库中有三个表:

1.Design_Master_ParentMenus
    1.ParentMenuID      1--------
    2.Title                     |
2.Design_Master_Categories      |
    1.CategoryID         1------|---------
    2.Title                     |        |
    3.ParentMenuID        *------        |
3.Design_Master_TileItem                 |
    1.TileItemID                         | 
    2.Title                              |
    3.CategoryID          *---------------

现在我想从Master_Design_TileItem中获取所有其祖父级的ParentMenuID等于1的项目。

到目前为止,我尝试了以下查询但没有成功。

var g = from f in db.Design_Master_Categories
        where f.CategoryID == 1
        select f.CategoryID;

var v = from h in db.Design_Master_ParentMenus
        where h.ParentMenuID == g.FirstOrDefault()
        select h.ParentMenuID;

var result = from t in db.Design_Master_TileItem
             join c in db.Design_Master_Categories
             on t.CategoryID equals c.CategoryID
             join p in db.Design_Master_ParentMenus
             on c.ParentMenuID equals p.ParentMenuID
             where p.ParentMenuID == v.FirstOrDefault()
             select t;

但是当我运行程序时,总是得到result = null的结果。

你是在尝试确定一个有效的ID吗?同样,声明vg时,它们将始终为1。查询语句为:where f.CategoryID == 1 select f.CategoryID - Brad M
@BradM,你在上面的评论中是什么意思? - Vishal
2个回答

3

如何实现

    where f.CategoryID == 1

条件如下:

现在我想从 Master_Design_TileItem 中获取所有祖先的 ParentMenuID 等于 1 的项目。

你说你想要 ParentMenuID = 1,而不是 CategoryID = 1

你也没有说明你的模型设置如何,但是使用适当的导航属性,你应该能够做到:

var result = from c in db.Design_Master_Categories
             from i in c.TileItems
             where c.ParentMenuID == 1
             select i;

你的查询有效,你的问题的答案是我有一个ID = 1的TileItem。我知道它的ParentID也是1,它的祖父ID也是1。所以我直接问了它。是的,谢谢你快速回复。 - Vishal

0
如果您的数据库设置正确,那么这应该很容易:
Design_Master_TileItems.Where(x => x.Design_Master_Category.ParentMenuId == 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接