Rob
Rob

Reputation: 193

c# mvc apicontroller five table join and return value

can anyone please tell me, I have already joined five tables and I have tested join is correct. When I debug up to "Location1" values return.

Also I added return Location1, It says

"Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)"

My question is how can I display return valve?

This is my ApiController and class code.

 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;
	
              

           }


       }

Upvotes: 2

Views: 175

Answers (1)

Aniket Inge
Aniket Inge

Reputation: 25705

Write like this:

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();

You'll have to explicitly convert it to IEnumerable<Post> instead of an anonymous type.

Upvotes: 1

Related Questions