Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# mvc4在检索json时添加where子句_C#_Asp.net Mvc_Json_Asp.net Mvc 4 - Fatal编程技术网

C# mvc4在检索json时添加where子句

C# mvc4在检索json时添加where子句,c#,asp.net-mvc,json,asp.net-mvc-4,C#,Asp.net Mvc,Json,Asp.net Mvc 4,我想将json从我的控制器检索到我的模型 控制器将从数据库检索数据,然后将其传输到json 我试过这个 return this.Json( new { Result = (from obj in db.Parkings select new { ID = obj.ID, Name = obj.note }) } , JsonRequestBe

我想将json从我的控制器检索到我的模型

控制器将从数据库检索数据,然后将其传输到json

我试过这个

    return this.Json(
              new
              {
                  Result = (from obj in db.Parkings select new { ID = obj.ID, Name = obj.note })
              }
              , JsonRequestBehavior.AllowGet
           );
它工作得很好

现在我想编辑它,以便将
where
添加到检索操作中

我的意思是:

我不想检索所有停车场,但我想检索buildingID等于1的停车场
我试过谷歌,但我自己也无法找到解决方案。简单地说,在dbset之后添加where方法。详情如下:

return this.Json(
          new
          {
              Result = (from obj in db.Parkings
                                      .Where(p => p.BuildingId == myBuildingId)
                        select new
                        {
                            ID = obj.ID,
                            Name = obj.note
                        })
          }
          , JsonRequestBehavior.AllowGet
       );
您还可以使用以下语法:

    (from obj in db.Parkings 
          where obj.BuildingId == myBuildingId
          select new { ID = obj.ID, Name = obj.note })
          }
          , JsonRequestBehavior.AllowGet
       );  

我建议您在中查看并查看类似的lambda语法,以便练习。

简单地说,在dbset之后添加where方法。详情如下:

return this.Json(
          new
          {
              Result = (from obj in db.Parkings
                                      .Where(p => p.BuildingId == myBuildingId)
                        select new
                        {
                            ID = obj.ID,
                            Name = obj.note
                        })
          }
          , JsonRequestBehavior.AllowGet
       );
Result = Parkings.Where(x=> x.buildingID == 1).Select(new { ID = obj.ID, Name = obj.note });
您还可以使用以下语法:

    (from obj in db.Parkings 
          where obj.BuildingId == myBuildingId
          select new { ID = obj.ID, Name = obj.note })
          }
          , JsonRequestBehavior.AllowGet
       );  
我建议您在练习中查看类似的lambda语法。

您阅读了吗?您阅读了吗?
Result = Parkings.Where(x=> x.buildingID == 1).Select(new { ID = obj.ID, Name = obj.note });