如何访问类内的列表并作为 asp.netcore 中的项目列表返回?

发布于 2025-01-14 05:08:48 字数 1935 浏览 0 评论 0原文

我有一个带有销售信息列表的班级展示预订。

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

在调用更新方法时,我需要更新 SeatNo 和 SalesOrderConfirmationId,但我在 for 循环内无法访问此列表。我需要在更新 showid、seatno SalesOrderConfirmationId 后返回项目列表,但 item.SalesInformation = salesInfo 显示错误无法转换销售信息到通用列表。请让我知道如何解决这个问题

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗地喜欢 2025-01-21 05:08:48

“SalesInformation”是一个列表,而 salesInfo 是一个对象。您需要执行以下操作:

item.SalesInformation.Add(salesInfo);

另外,在 ShowBooking 的构造函数内,初始化您的列表,否则您将收到 NullReferenceException。

"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.

忘你却要生生世世 2025-01-21 05:08:48

尝试使用

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

ShowBookingsSalesInformationList 类型,因此需要使用列表来设置其值。

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.

不顾 2025-01-21 05:08:48

您已经有演出预订列表

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

you already have list of showbookings

public async Task<ActionResult<List<ShowBookings>>> UpdateShowBookings(string ShowId, string SeatNo, string SalesOrderConfirmationId)
          {            
             
              var salesInformation = new List<SalesInformation>();          
              var showBookings = await _context.DbCollection.Find(ShowBookings => ShowBookings.ShowId == ShowId)
                             .ToListAsync().ConfigureAwait(false);           
              if (showBookings != null)
              {                       
                return showBookings;
              }
            else
            {
                return new List<ShowBookings>();
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文