如何在odata控制器中对IQueryable <t>执行LINQ连接

时间:2017-12-30 07:55:00

标签: linq asp.net-web-api odata iqueryable

我有一个WebAPI方法,它使用OData查询选项将数据返回给客户端。该实体具有cityid,我希望使用连接来自另一个实体的cityname。

我尝试使用下面的Api方法,这是不完整的。

ENTITY1: -

 public partial class UU_DeliveryCharges
    {
        public int DeliveryChargeId { get; set; }
        public Nullable<int> CityId { get; set; }
        public Nullable<int> VehicleTypeId { get; set; }
        public Nullable<decimal> MileRate { get; set; }
        public Nullable<decimal> FlatRate { get; set; }
        public Nullable<decimal> FlatMile { get; set; }
        public Nullable<decimal> PickUpFee { get; set; }
        public Nullable<decimal> DropOffFee { get; set; }
        public Nullable<int> CreateBy { get; set; }
        public Nullable<System.DateTime> SysDate { get; set; }
        public Nullable<bool> Status { get; set; }
    }

ENTITY2: -

 public partial class SC_Cities
    {
        public int CityId { get; set; }
        public Nullable<int> StateId { get; set; }
        public string CityName { get; set; }
        public Nullable<bool> CityStatus { get; set; }
    }

WebApi方法: -

    public IQueryable<UU_DeliveryCharges> GetUU_DeliveryCharges()
            {
                //var aaa= db.UU_DeliveryCharges;
                //return aaa;

                var results = (from deliveries in db.UU_DeliveryCharges
                                     join vehicles in db.UU_VehicleTypes on deliveries.VehicleTypeId equals vehicles.VehicleTypeId
                                     join cities in db.SC_Cities on deliveries.CityId equals cities.CityId
                                     join states in db.SC_States on cities.StateId equals states.StateId
                                     join countries in db.SC_Countries on states.CountryId equals countries.CountryId
                                     where (deliveries.Status == true)
                                     select new { deliveries = deliveries.FlatMile, deliveries.MileRate, deliveries.PickUpFee, deliveries.DropOffFee,
                                       vehicles=vehicles.VehicleType, cities = cities.CityName, states = states.StateName, countries = countries.CountryName
                                     }).ToList();
           return results;
         }

还尝试创建一个新类,如下所示但没有正确的方法,TIA。

 public partial class DeliveryCharges
    {
        public string ReturnCode { get; set; } //-1:Error/0:missing or validation /1:success
        public string ReturnMessage { get; set; }
        public string CountryName { get; set; }
        public string StateName { get; set; }
        public string CityName { get; set; }
        public string VehicleName { get; set; }
        public Nullable<decimal> MileRate { get; set; }
        public Nullable<decimal> FlatRate { get; set; }
        public Nullable<decimal> FlatMile { get; set; }
        public Nullable<decimal> PickUpFee { get; set; }
        public Nullable<decimal> DropOffFee { get; set; }
    }

修改: -

我已经用下面的方法替换了Api方法,而后者依次评估连接但不返回结果。

public IQueryable<DeliveryCharges> GetUU_DeliveryCharges()
        {
            //var aaa= db.UU_DeliveryCharges;
            //return aaa;
            var results = from deliveries in db.UU_DeliveryCharges
                          join vehicles in db.UU_VehicleTypes on deliveries.VehicleTypeId equals vehicles.VehicleTypeId
                          join cities in db.SC_Cities on deliveries.CityId equals cities.CityId
                          join states in db.SC_States on cities.StateId equals states.StateId
                          join countries in db.SC_Countries on states.CountryId equals countries.CountryId
                          where (deliveries.Status == true)
                          select new DeliveryCharges
                          {
                              FlatRate = deliveries.FlatRate,
                              MileRate = deliveries.MileRate,
                              PickUpFee = deliveries.PickUpFee,
                              DropOffFee = deliveries.DropOffFee,
                              CityName = cities.CityName
                              //UU_DeliveryCharges = deliveries.FlatMile, deliveries.MileRate, deliveries.PickUpFee, deliveries.DropOffFee
                              //,vehicles = vehicles.VehicleType, cities = cities.CityName, states = states.StateName, countries = countries.CountryName
                          };
return results;
}

WebApiConfig: -

   public static void Register(HttpConfiguration config)
        {
     ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<UU_DeliveryCharges>("UU_DeliveryCharges");
            config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
}

0 个答案:

没有答案