简体   繁体   中英

How can i apply LINQ query on nested collections by checking each collection is null or not using LINQ with asp.net C#?

How can i access the nested collection by checking null or not using LINQ with asp.net c# ? As i am getting null exception when there is no mealPlan?

As i am getting null exception when there is no mealPlan?

Part of code:-

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

Yes you'll get NullReferenceException. Try to use join if you want to exclude rows with null values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM