Get all children whose grandparents identifier is 1

I have three tables in the database:

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          *---------------

Now I want to get all the elements from Master_Design_TileItemwhose grandparent ParentMenuID is 1.

So far I tried the following queries, but I failed.

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;

But when I run the program, I always get result = null.

+4
source share
2 answers

how

    where f.CategoryID == 1

should

Now I want to get all the elements from Master_Design_TileItemwhose grandparent is 1 . ParentMenuID

? You said you want ParentMenuID = 1, not CategoryID = 1.

You also did not say how your model is configured, but with the proper navigation properties, you can:

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

, , :

Design_Master_TileItems.Where(x => x.Design_Master_Category.ParentMenuId == 1)
0

Source: https://habr.com/ru/post/1530694/


All Articles