如何在odata控制器中对IQueryable操作执行LINQ连接

时间:2017-12-30 12:08:29

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

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

我尝试使用下面的Api方法,后者反过来评估连接但不返回结果。

ENTITY1: -

public class MonitorTest {
  @MockBean
  private IMonospaceNotifier notifier;

  @Test
  public void doNothing(){
    doReturn(SOME_TEST_REPORT_STRING).when(notifier).monospace(anyString());
  // ...

ENTITY2: -

 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; }
    }

分类: -

 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 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; }
    }

WebAPIConfig: -

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;
}

0 个答案:

没有答案