c# MVC - apicontroller五表连接和返回值

本文关键字:连接 返回值 MVC apicontroller | 更新日期: 2023-09-27 17:50:29

谁能告诉我,我已经加入了五个表,我已经测试了加入是正确的。当我调试到"Location1"值返回。

我还添加了return Location1,它显示

"错误1不能隐式转换类型"System.Collections.Generic。列表"System.Collections.Generic.IEnumerable"。存在显式转换(您是否缺少强制转换?)"

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

这是我的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;
	
           }
       }

c# MVC - apicontroller五表连接和返回值

这样写:

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>而不是匿名类型。