Home > OS >  How to access a list inside a class and return as a list of items in asp.netcore?
How to access a list inside a class and return as a list of items in asp.netcore?

Time:03-16

I have a class showbookings with salesinformation list.

public class ShowBookings
        {
    
            [BsonId]
            [BsonRepresentation(BsonType.ObjectId)]
            public string Id { get; set; }
            public string ShowId { get; set; }
            public DateTime ShowTime { get; set; }
            public List<SalesInformation> SalesInformation { get; set; }
    
        }
            public class SalesInformation
            {
                public string SeatNo { get; set; }
                public string SalesOrderConfirmationId { get; set; }
            }

While calling update method I need to update for SeatNo and SalesOrderConfirmationId but this list is not accessible to me inside for loop.I need to return the list of items after updating showid,seatno SalesOrderConfirmationId but item.SalesInformation = salesInfo shows an error cannot convert salesinformation to generic list.Pls let me know how to sort this issue

public async Task<ActionResult<List<ShowBookings>>> UpdateShowBookings(string ShowId, string SeatNo, string SalesOrderConfirmationId)
          {            
              var showBookings = new List<ShowBookings>();
              var salesInformation = new List<SalesInformation>();          
              showBookings = await _context.DbCollection.Find(ShowBookings => ShowBookings.ShowId == ShowId)
                             .ToListAsync().ConfigureAwait(false);           
              if (showBookings != null)
              {
                  var _sbList = showBookings.ToList();
                foreach (var item in _sbList)
                {
                    var salesInfo = new SalesInformation();
                    item.ShowId = ShowId;
                    salesInfo.SeatNo = SeatNo;
                    item.SalesInformation = salesInfo;
                    _sbList.Add(item);
                }              
                return _sbList;
              }
            else
            {
                return new List<ShowBookings>();
            }
        }

CodePudding user response:

"SalesInformation" is a list, while salesInfo is a object. You need to do:

item.SalesInformation.Add(salesInfo);

Also, in ShowBooking, inside constructor, initialize your List, else you will receive NullReferenceException.

CodePudding user response:

Try to use

item.SalesInformation = new List<SalesInformation> { salesInfo };

SalesInformation of ShowBookings is List<SalesInformation> type,so you need to set its value with a list.

  • Related