Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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
ASP.Net MVC Web API返回对象列表_Asp.net_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

ASP.Net MVC Web API返回对象列表

ASP.Net MVC Web API返回对象列表,asp.net,asp.net-mvc,asp.net-web-api,Asp.net,Asp.net Mvc,Asp.net Web Api,如何正确返回“CarTypes”对象列表(从第二个方法),其中传入的TyreID不是CarType类的主键-例如,我想返回所有CarTypes的列表,其中TyreID为5: // GET api/CarTypes public IEnumerable<CarTypes> GetCarTypes() { return db.CarTypes.AsEnumerable(); //This works fineCar } // GET api/CarTypes/5 public

如何正确返回“CarTypes”对象列表(从第二个方法),其中传入的TyreID不是CarType类的主键-例如,我想返回所有CarTypes的列表,其中TyreID为5:

// GET api/CarTypes
public IEnumerable<CarTypes> GetCarTypes()
{
    return db.CarTypes.AsEnumerable();  //This works fineCar
}

// GET api/CarTypes/5
public IEnumerable<CarTypes> GetCarTypes(long id)
{
    CarTypes cartypes = db.CarTypes.Select(t => t.TyreID == id).AsEnumerable();
    if (roomtypes == null)
    {
        throw new HttpResponseException(Request
            .CreateResponse(HttpStatusCode.NotFound));
    }

    return cartypes;
}
//获取api/CarTypes
公共IEnumerable GetCarTypes()
{
return db.CarTypes.AsEnumerable();//这很有效
}
//获取api/CarTypes/5
公共IEnumerable GetCarTypes(长id)
{
CarTypes CarTypes=db.CarTypes.Select(t=>t.TyreID==id).AsEnumerable();
if(roomtypes==null)
{
抛出新的HttpResponseException(请求
.CreateResponse(HttpStatusCode.NotFound));
}
返回cartypes;
}
它当前显示错误:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“MVCAPApplication4.Models.CarTypes”。存在显式转换(是否缺少强制转换?)

如果我在查询中使用Select/SelectMany/Where有关系吗?

您应该使用“Where”而不是“Select”

“选择”用于指定每个记录应返回哪些数据,而不是用于筛选记录。使用“Select”的查询将返回布尔值:对于TyreID为的记录为false!=对于TyreID=id:)的一条记录,id和true应使用“where”而不是“Select”

“选择”用于指定每个记录应返回哪些数据,而不是用于筛选记录。使用“Select”的查询将返回布尔值:对于TyreID为的记录为false!=一条记录的id和true,其中TyreID=id:)

您不应该有:

IEnumerable<CarTypes> cartypes = db.CarTypes.Where(t => t.TyreID == id).AsEnumerable();
注意:我本想在PanJanek的回答下发表这一评论,但由于我的名声不好,我现在不被允许…

你不应该:

IEnumerable<CarTypes> cartypes = db.CarTypes.Where(t => t.TyreID == id).AsEnumerable();

注意:我本想在PanJanek的回答下发表这一评论,但由于我的名声不好,我现在不被允许…

首先,你需要使用
Where
而不是
Select
;其次,在将AsEnumerable()更改为Where之后,不需要使用它,但可能需要调用ToList(),以便Linq2Sql/EntityFramework在将值返回到视图之前执行查询

 // GET api/CarTypes/5
    public IEnumerable<CarTypes> GetCarTypes(long id)
    {
        var cartypes = db.CarTypes.Where(t => t.TyreID == id).ToList();
        if (cartypes == null || !cartypes.Any())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return cartypes;
    }
//获取api/CarTypes/5
公共IEnumerable GetCarTypes(长id)
{
var cartypes=db.cartypes.Where(t=>t.TyreID==id.ToList();
if(cartypes==null | |!cartypes.Any())
{
抛出新的HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
返回cartypes;
}

在查询执行后,我还添加了一个附加检查,但您可能不需要它,这取决于您希望如何处理空集合。

首先,您需要使用
Where
而不是
Select
;其次,在将AsEnumerable()更改为Where之后,不需要使用它,但可能需要调用ToList(),以便Linq2Sql/EntityFramework在将值返回到视图之前执行查询

 // GET api/CarTypes/5
    public IEnumerable<CarTypes> GetCarTypes(long id)
    {
        var cartypes = db.CarTypes.Where(t => t.TyreID == id).ToList();
        if (cartypes == null || !cartypes.Any())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return cartypes;
    }
//获取api/CarTypes/5
公共IEnumerable GetCarTypes(长id)
{
var cartypes=db.cartypes.Where(t=>t.TyreID==id.ToList();
if(cartypes==null | |!cartypes.Any())
{
抛出新的HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
返回cartypes;
}

在查询执行后,我还添加了一个附加检查,但您可能不需要它,这取决于您希望如何处理空集合。

另外,您不想返回cartypes.AsEnumerable()Hi-我在Linq语句末尾有.AsEnumerable()-但无论我是否添加cartypes.AsEnumerable(),错误仍然保留在linq语句上。再次感谢您,Mark。另外,您不想返回cartypes.AsEnumerable()Hi-我在Linq语句的末尾有.AsEnumerable()-但是无论我是否添加cartypes.AsEnumerable(),错误仍然保留在Linq语句上。再次感谢,马克。非常感谢-这是我错过的(在队伍开始时IEnumerable)-潘珍尼克/迈克-也感谢你的帮助-干杯,马克非常感谢-这是我错过的(在队伍开始时IEnumerable)-潘珍尼克/迈克-也感谢你的帮助-干杯,MarkHi@WestDiscGolf-VS(2012 RC)显示db.CarTypes上的错误。。。无法将类型“System.Collections.Generic.List”隐式转换为“MVCAPApplication4.Models.cartypesHi@WestDiscGolf”,这可能太难评论,但对上述实现有疑问,如果您查看API,它会返回一个列表,但我们将IEnumerable作为该方法的返回类型。上述实施将获得成功。。毫无疑问。我的问题是,1)可以互换使用这两种方法吗2)当然,我们返回的是一个列表,那么将返回类型设为list比IEnumerableHi@TechQuery有什么害处呢,所以它不是那么“可互换”,但事实上list实现了IEnumerable(),但也做了很多事情。如果你返回一个接口,那么实现它的任何东西都可以用来使用它,但是如果使用一个更具体的实现,比如列表,那么它会限制可以使用的东西。这很有趣,但是你的“cartypes”永远不会为空。它始终是一个包含零个或多个元素的列表。Hi@WestDiscGolf-VS(2012 RC)在db.CarTypes上显示错误。。。无法将类型“System.Collections.Generic.List”隐式转换为“MVCAPApplication4.Models.cartypesHi@WestDiscGolf”,这可能太难评论,但对上述实现有疑问,如果您查看API,它会返回一个列表,但我们将IEnumerable作为该方法的返回类型。上述实施将获得成功。。毫无疑问。我的问题是,1)可以互换使用这两种方法吗2)当然,我们返回的是一个列表,那么将返回类型设为list而不是IEnumerableHi@TechQuery有什么害处,所以它不是那么“可互换”,而是