c#mvc apicontroller五表连接和返回值

时间:2015-05-24 04:29:21

标签: c# asp.net-mvc asp.net-mvc-4

任何人都可以告诉我,我已经加入了五张桌子,我测试过加入是正确的。当我调试到" Location1"价值回报。

我还添加了返回Location1,它说

  

"错误1无法隐式转换类型   ' System.Collections.Generic.List'至   ' System.Collections.Generic.IEnumerable&#39 ;.   存在显式转换(您是否错过了演员?)"

我的问题是如何显示回流阀?

这是我的ApiController和类代码。



 public class Posts
    {
       
        public Suburb suburb { get; set; }
        public SubRegion subRegion { get; set; }
        public SubRegionDeliveryTime subRegionDeliveryTime { get; set; }
        public DeliveryTime deliveryTime { get; set; }
        public DeliveryPeriod deliveryPeriod { get; set; }

    }



 public IEnumerable<Posts> Get()
            
       {
         
           using (ApplicationDbContext db = new ApplicationDbContext())
           {
              
             var  Location1 = (from su in db.TLCSuburb
                                join Subr in db.TLCSubRegion on
                                su.SubRegionID equals Subr.SubregionID
                                join srdt in db.TLCSubRegionDeliveryTime on
                                Subr.SubregionID equals srdt.SubregionID
                                join DT in db.TLCDeliveryTime on
                                srdt.DeliveryTimeId equals     DT.DeliveryTimeId
                                join DP in db.TLCDeliveryPeriod on
                                DT.DeliveryPeriodID equals DP.DeliveryPeriodID
                                orderby Subr.SubregionID
                                select new 
                                {
                                    su.name,
                                    su.postcode,
                                    su.AuState,
                                    su.Latitude,
                                    su.Longitude,
                                    DT.DeliveryDay,
                                    DP.PeriodType,
                                    Subr.CloseDayId,
                                    Subr.SubregionName,
                                }).ToList();

             
            //return null;
            

            return Location1;
	
              

           }


       }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

像这样写:

var  Location1 = (from su in db.TLCSuburb
                                join Subr in db.TLCSubRegion on
                                su.SubRegionID equals Subr.SubregionID
                                join srdt in db.TLCSubRegionDeliveryTime on
                                Subr.SubregionID equals srdt.SubregionID
                                join DT in db.TLCDeliveryTime on
                                srdt.DeliveryTimeId equals     DT.DeliveryTimeId
                                join DP in db.TLCDeliveryPeriod on
                                DT.DeliveryPeriodID equals DP.DeliveryPeriodID
                                orderby Subr.SubregionID
                                select new Post
                                {
                                    suburb = new Suburb(){
                                       Name = su.name,
                                       PostCode = su.postcode,
                                       AuState = su.AuState,
                                       Latitude = su.Latitude,
                                       Longitude = su.Longitude
                                     }, 
                                    deliveryTime = DT.DeliveryDay,
                                    deliveryPeriod = new DeliveryPeriod(){
                                        PeriodType = DP.PeriodType
                                     },
                                    subRegion = new SubRegion(){ 
                                        CloseDayId = Subr.CloseDayId,
                                        SubRegionName = Subr.SubregionName
                                     }
                                }).ToList();

您必须将其显式转换为IEnumerable<Post>而不是匿名类型。