我如何通过检查每个集合为空或不使用LINQ在asp.net c#中应用LINQ查询嵌套集合

本文关键字:集合 LINQ net asp 嵌套 查询 应用 检查 何通过 | 更新日期: 2023-09-27 18:16:33

如何通过检查null或不使用LINQ与asp.net c#访问嵌套集合?当没有mealPlan时,我得到null异常?

当没有mealPlan时,我得到null异常?

部分代码:-

var hotelRoomBooking = (from n in roomsPart.DefaultIfEmpty()
                                        //where n.roomRates != null
                                        from rates in n.roomRates.DefaultIfEmpty()
                                        //where rates.roomRate != null
                                        from roomR in rates.roomRate.DefaultIfEmpty()
                                        //where roomR.mealPlans != null
                                        from mealP in roomR.mealPlans.mealPlan
                                        //where mealP.value != null
                                        from link in roomR.links.link
                                        select new
                                        {
                                            n.id,
                                            n.rooms.checkInDate,
                                            n.rooms.checkOutDate,`enter code here`
                                            n.rooms.totalCostOfRooms.totalCostInclusive.currency,
                                            roomR.averageNightlyRate.value,
                                            MealPlan = mealP.value != null ? mealP.value : string.Empty,
                                            n.stayDetails.noOfRooms,
                                            n.location.city,
                                            n.location.code
                                        }).ToList();

我如何通过检查每个集合为空或不使用LINQ在asp.net c#中应用LINQ查询嵌套集合

是的,你会得到NullReferenceException。如果您想要排除null值的行,请尝试使用join