连接表结果集后,MVC WEB API中的正确返回类型

时间:2013-11-17 22:23:55

标签: asp.net-mvc asp.net-web-api

这适用于我的网络API:

    public IEnumerable<Object> Get()
    {
        var passengerVehicle =
            from vehMake in _Uow.GetRepository<VehicleMake>().Include<VehicleModel>()
            join passVeh in _Uow.GetRepository<PassengerVehicle>().Include<Vehicle>()
                .Include(y => y.Vehicle.VehicleModel)
                .Include(y => y.Vehicle.VehicleSubModel)
                .Include(y => y.Vehicle.VehicleImages)
                .Include(y => y.Vehicle.VehicleLocation)
                .Include(y => y.Vehicle.VehiclePrices)
                on vehMake.Id equals passVeh.Vehicle.VehicleModel.FK_VehicleMakeId
            select new
            {
                vehMake = vehMake.Name,
                vehMod = passVeh.Vehicle.VehicleModel.Name,
                vehSubModel = passVeh.Vehicle.VehicleSubModel.Name,
                vehLocation = passVeh.Vehicle.VehicleLocation,
                vehImages = passVeh.Vehicle.VehicleImages.ToList(),
                vehOptions = passVeh.Vehicle.VehicleOptions.ToList(),
                vehPostHistories = passVeh.Vehicle.VehiclePostHistories.ToList(),
                vehStats = passVeh.Vehicle.VehicleStatistics.ToList(),
                vehStatus = passVeh.Vehicle.VehicleStatus,
                vehPrices = passVeh.Vehicle.VehiclePrices.ToList(),
            };

        return passengerVehicle;
    }

我不高兴返回类型是“对象”

我想也许是一个HttpResponse,但显然这也不正确。

PassengerVehicle与Vehicle有关联。

车辆与VehicleModel&amp; VehicleSubModel。

VehicleModel与VehicleMake有关联。

几乎所有其他东西都是原始的或直接的收藏品。

建议?

1 个答案:

答案 0 :(得分:0)

最好使用ViewModel,例如:

select new VehicleSpecialViewModel
        {
            vehMake = vehMake.Name,
            vehMod = passVeh.Vehicle.VehicleModel.Name,
            vehSubModel = passVeh.Vehicle.VehicleSubModel.Name,
            vehLocation = passVeh.Vehicle.VehicleLocation,
            vehImages = passVeh.Vehicle.VehicleImages.ToList(),
            vehOptions = passVeh.Vehicle.VehicleOptions.ToList(),
            vehPostHistories = passVeh.Vehicle.VehiclePostHistories.ToList(),
            vehStats = passVeh.Vehicle.VehicleStatistics.ToList(),
            vehStatus = passVeh.Vehicle.VehicleStatus,
            vehPrices = passVeh.Vehicle.VehiclePrices.ToList(),
        };

然后你的返回类型将是

IEnumerable<VehicleSpecialViewModel>