Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 让所有祖父母';s ID等于1_C#_Sql_Linq_Entity Framework - Fatal编程技术网

C# 让所有祖父母';s ID等于1

C# 让所有祖父母';s ID等于1,c#,sql,linq,entity-framework,C#,Sql,Linq,Entity Framework,我在数据库中有三个表,如下所示: 1.Design_Master_ParentMenus 1.ParentMenuID 1-------- 2.Title | 2.Design_Master_Categories | 1.CategoryID 1------|--------- 2.Title | | 3.ParentMenuI

我在数据库中有三个表,如下所示:

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\u Design\u 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。

怎么做

    where f.CategoryID == 1
情况如下

现在我想从
Master\u Design\u 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;

如果已正确设置数据库,则应尽可能简单:

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

其中f.CategoryID==1选择f.CategoryID
。您是否正在尝试确定是否存在有效的ID?与声明
v
g
相同,它们将始终是
1
@BradM您的上述评论是什么意思?您的查询有效,您的问题的答案是我有一个ID=1的TileItem。我知道它的父ID也是1,它的父ID也是1。所以我直接问了。是的,谢谢你的快速回复。